Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to check if android WindowManager already contains a view?

When I try to do a WindowManager.removeView(),

E/AndroidRuntime( 2445): java.lang.IllegalArgumentException: View=android.widget.LinearLayout{41a03700 V.E..... ......I. 0,0-0,0} not attached to window manager

E/AndroidRuntime( 2445):             at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:370)

E/AndroidRuntime( 2445):             at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:299)

E/AndroidRuntime( 2445):             at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:79)

I get this fatal error because the view was not in the window manager. Is there no way to check if windowmanager had already added the view before? I do not see any such method in the source

like image 653
likejudo Avatar asked Nov 20 '14 16:11

likejudo


2 Answers

You can check to see if the view's window token is null:

if(view.getWindowToken() != null){
    WindowManager.removeView(view);
}

You could also just catch the exception:

try{
    WindowManager.removeView(view);
}catch(IllegalArgumentException e){
    Log.e(debug_tag, "view not found");
}
like image 181
JstnPwll Avatar answered Nov 06 '22 14:11

JstnPwll


Its also a better way to check its already added on window or not.

if (view.getParent() != null)) {
    windowsManager.removeView(view);
}
like image 43
Abeer Iqbal Avatar answered Nov 06 '22 13:11

Abeer Iqbal