i'm trying to add and remove view with this code:
public class window {
public static void enable(Context context){
int LAYOUT_FLAG;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
LAYOUT_FLAG,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(R.layout.testview, null);
wm.addView(myView, params);
}
public static void disable(Context context)
{
WindowManager wm = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(R.layout.testview, null);
wm.removeView(myView);
}
}
and i'll call methods on another class, which has the following code:
@Override
public void onActivityStarted(Activity activity) {
if (++activityReferences == 1 && !isActivityChangingConfigurations) {
// App enters foreground
window.enable(activity);
}
}
@Override
public void onActivityStopped(Activity activity) {
isActivityChangingConfigurations = activity.isChangingConfigurations();
if (--activityReferences == 0 && !isActivityChangingConfigurations) {
// App enters background
window.disable(activity);
}
}
And the result is error on disable method ( not attached to window manager) with the following error :
java.lang.RuntimeException: Unable to stop activity {com.main.test/com.main.test.MainActivity}: java.lang.IllegalArgumentException: View=android.widget.RelativeLayout{4f192bd V.E...... ......ID 0,0-0,0 #7f0a11b7 app:id/view} not attached to window manager
at android.app.ActivityThread.callActivityOnStop(Unknown Source:104)
at android.app.ActivityThread.performStopActivityInner(Unknown Source:135)
at android.app.ActivityThread.handleStopActivity(Unknown Source:27)
at android.app.servertransaction.StopActivityItem.execute(Unknown Source:15)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(Unknown Source:32)
at android.app.servertransaction.TransactionExecutor.execute(Unknown Source:76)
at android.app.ActivityThread$H.handleMessage(Unknown Source:52)
at android.os.Handler.dispatchMessage(Unknown Source:19)
at android.os.Looper.loop(Unknown Source:249)
at android.app.ActivityThread.main(Unknown Source:134)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(Unknown Source:11)
at com.android.internal.os.ZygoteInit.main(Unknown Source:313)
Caused by: java.lang.IllegalArgumentException: View=android.widget.RelativeLayout{4f192bd V.E...... ......ID 0,0-0,0 #7f0a11b7 app:id/view} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(Unknown Source:35)
at android.view.WindowManagerGlobal.removeView(Unknown Source:6)
at android.view.WindowManagerImpl.removeView(Unknown Source:3)
at com.main.test.security.window.disable(window.java:47)
at com.main.test.GSYApplication.onActivityStopped(GSYApplication.java:194)
at android.app.Application.dispatchActivityStopped(Unknown Source:14)
at android.app.Activity.dispatchActivityStopped(Unknown Source:25)
at android.app.Activity.onStop(Unknown Source:13)
at androidx.fragment.app.FragmentActivity.onStop(FragmentActivity.java:558)
at androidx.appcompat.app.AppCompatActivity.onStop(AppCompatActivity.java:216)
at android.app.Instrumentation.callActivityOnStop(Unknown Source:0)
at android.app.Activity.performStop(Unknown Source:59)
at android.app.ActivityThread.callActivityOnStop(Unknown Source:36)
at android.app.ActivityThread.performStopActivityInner(Unknown Source:135)
at android.app.ActivityThread.handleStopActivity(Unknown Source:27)
at android.app.servertransaction.StopActivityItem.execute(Unknown Source:15)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(Unknown Source:32)
at android.app.servertransaction.TransactionExecutor.execute(Unknown Source:76)
at android.app.ActivityThread$H.handleMessage(Unknown Source:52)
at android.os.Handler.dispatchMessage(Unknown Source:19)
at android.os.Looper.loop(Unknown Source:249)
at android.app.ActivityThread.main(Unknown Source:134)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(Unknown Source:11)
at com.android.internal.os.ZygoteInit.main(Unknown Source:313)
if i add try and catch app doesn't crash anymore, but view still showing when app is in background how can i fix this?
Thank you for your help