Activity has leaked window - Android

前端 未结 5 1326
情歌与酒
情歌与酒 2020-12-05 07:21

Look at these pieces of code:

Custom views and window attributes on Android

Problem

When I click \'Home button\', exception

相关标签:
5条回答
  • 2020-12-05 07:44

    Check weather the dialog is showing or not

    @Override
      protected void onStop() {
        super.onStop();
    if (loadingDlg != null) {
    
            if(loadingDlg.isShowing())
            loadingDlg.dismiss();
    
            loadingDlg = null;
     }
    }
    
    0 讨论(0)
  • 2020-12-05 07:53

    You're trying to show a Dialog after you've exited an Activity.

    The solution is to call dismiss() on the Dialog you created before exiting the Activity, e.g. in onPause(). All windows&dialogs should be closed before leaving an Activity.

    @Override
     protected void onStop() {
      super.onStop();
      if (loadingDlg != null) {
       loadingDlg.dismiss();
       loadingDlg = null;
      }
    }
    

    Hope it should helpful for you.

    Activity-has-leaked-window-that-was-originally-added

    0 讨论(0)
  • 2020-12-05 07:53

    Leaked window usually happens when your context show dialogs and that context is suddenly forced close that your dialog has not been dismissed properly.

    In order to fix this, you have to fix your errors before the window has leaked error.

    0 讨论(0)
  • 2020-12-05 08:03

    What is a leak in programming?

    The memory that you acquire and do not release lead to the memory leak.Similar happens with the (windows/dialogs).

    What's happening here?

    You are trying to add a window and while it shows up it is on the foreground,but when you are pressing the home button it gets paused and then gets stopped (Try to put a toast in onStop() and onPause()).

    Since you did not tell the system to remove your view , hence it remains attached to the window that now has disappeared/detached from the application. Hence according to the system your customView occupied the space which it did not release.

    Solution

    Inside your onStop() or onPause()andonDestroy() make sure you dismiss your view(dismiss() if it's a dialog) or remove it(remove()if added using window Manager).

    Add the dismiss or remove functions inside your on unload function as you mentioned that on pressing back button you get this error.On exiting an app its onUnload() method gets called.

    Suggestion(Ignore if not in context)

    As i can observe you are trying to make a System alert window that comes over anything beneath it.Adding such kind of pop ups in the activity is risky as it may cause leakage problems. You may actually add such kind of window via a Service so it outlives your activity and shows up everywhere on the device (if that is what you need).

    Check this out

    Update 2-The Cordova lifecycle

    Why don't you try to override onUnload method in your CordovaPlugin class.I tried finding but the docs are mentioning the existence of onPause and onResume methods.If you got onUnload in the CordovaPlugin class then remove the view you are making in your view class runOnUiThread method.

    0 讨论(0)
  • 2020-12-05 08:05

    I had a finish() that was called place before my AlertDialog was called which caused the Activity to finish before the AlertDialog was called. I removed the finish() and put it after the user had completed their input using the AlertDialog and the window leak was fixed.

    0 讨论(0)
提交回复
热议问题