I'm trying to create an overlay window using the next code:
int OVERLAY_TYPE;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
OVERLAY_TYPE = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
OVERLAY_TYPE = WindowManager.LayoutParams.TYPE_PHONE;
}
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
ImageView popupView = new ImageView(this);
popupView.setImageResource(R.drawable.bottom_dialog_background);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
OVERLAY_TYPE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.BOTTOM;
windowManager.addView(popupView, params);
I've added <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
to Manifest
and allowed all needed permissions for that.
It works fine for Android versions up to Android Q. But Q and R versions don't work at the same way. Overlay window creates, persists through some activities and other apps. It even persists through Android Settings
but when I try to open something deeper than just Android Settings
, for example Usage Data Access
screen or App Data
screen, the Overlay Window disappears. If I press Back button
it will show the previous Activity and Overlay Window will be shown again.
Does anyone know how to "fix" it and achieve the same behavior for all Android versions? Any help would be highly appreciated. Thanks in advance.