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

android, android-layout

Solution

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");
}

Problem

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

Original source