Buneme Kyakilika
Buneme Kyakilika

Reputation: 1202

Android overlapping views when using WindowManager

I'm using WindowManager to add views to the users screen using an IntentService, as shown here:

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.BOTTOM | Gravity.CENTER;
    params.x = 0;
    params.y = 100;
    windowManager.addView(cardView, params);

When I first launch the IntentService, the view is created as expected. However, when I re-launch the IntentService, a new view is added but the previous view remains.

How can I remove all the views that I have already added before adding a new view? I know that if I kill the IntentService then the views disappear, but how do I delete all other instances of my IntentService except the instance that I have just launched?

This is what it looks like when I first call the IntentService: 2]

By the time the IntentService has been called multiple times, a shadow appears because there are so many view stacked on top of each other: 1]

Upvotes: 1

Views: 712

Answers (1)

Shreya
Shreya

Reputation: 31

When the service ends, you need to remove your previous views in the onDestroy() function.

if(cardview!=null){
 WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
            windowManager.removeView(mView);
}

Upvotes: 1

Related Questions