HelloCW
HelloCW

Reputation: 2295

Why isn't a window with TYPE_APPLICATION_OVERLAY closed when I finish a Activity?

I requires Manifest.permission.SYSTEM_ALERT_WINDOW for a Float window, and open the window from a Activity by launching FloatWindowManager.getInstance().applyOrShowFloatWindow(FloatWindowActivity.this);

The float window works well.

I think the float will close when I finish the Activity by launching finish();

But in fact, the float window still is diplaying even if Activity has been closed, why?

Main

public class FloatWindowActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_show_or_apply).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FloatWindowManager.getInstance().applyOrShowFloatWindow(FloatWindowActivity.this);
            }
        });

        findViewById(R.id.btn_dismiss).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FloatWindowManager.getInstance().dismissWindow();
            }
        });


        findViewById(R.id.btn_Close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

}

Float

public class FloatWindowManager { 
   ...

   private void showWindow(Context context) {
        if (!isWindowDismiss) {
            Log.e(TAG, "view is already added here");
            return;
        }

        isWindowDismiss = false;
        if (windowManager == null) {
            windowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        }

        Point size = new Point();
        windowManager.getDefaultDisplay().getSize(size);
        int screenWidth = size.x;
        int screenHeight = size.y;

        mParams = new WindowManager.LayoutParams();
        mParams.packageName = context.getPackageName();
        mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        int mType;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mType = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            mType = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        }
        mParams.type = mType;
        mParams.format = PixelFormat.RGBA_8888;
        mParams.gravity = Gravity.LEFT | Gravity.TOP;
        mParams.x = screenWidth - dp2px(context, 100);
        mParams.y = screenHeight - dp2px(context, 171);

        floatView = new AVCallFloatView(context);
        floatView.setParams(mParams);
        floatView.setIsShowing(true);
        windowManager.addView(floatView, mParams);
    }
    ...
}

Upvotes: 1

Views: 556

Answers (2)

RufusInZen
RufusInZen

Reputation: 2189

You need to manually remove the floatView using the windowManager:

//Check if the view is still attached to window, otherwise it will throw an exception
if (floatView.getParent() != null) {
    windowManager.removeView(floatView);
}

Upvotes: 0

Shaon
Shaon

Reputation: 2724

You have drawn the float window over other window or view. When it starts it has no dependency. So I will suggest you to make the visibility gone.

Create a function inside the float window manager and hide the visibility

floatView.setVisibility(View.GONE);

I think it will work.

Upvotes: 1

Related Questions