Activity has leaked window that was originally added

后端 未结 30 3048
野趣味
野趣味 2020-11-21 05:48

What is this error, and why does it happen?

05-17 18:24:57.069: ERROR/WindowManager(18850): Activity com.mypkg.myP has leaked window com.android.internal.pol         


        
相关标签:
30条回答
  • 2020-11-21 06:17

    Window leaked exceptions have two reasons:

    1) showing the dialog when Activity Context doesn't exists, to solve this you should show the dialog only you are sure Activity exists:

    if(getActivity()!= null && !getActivity().isFinishing()){
            Dialog.show();
    }
    

    2) not dismiss the dialog appropriately, to solve use this code:

    @Override
    public void onDestroy(){
        super.onDestroy();
        if ( Dialog!=null && Dialog.isShowing() ){
            Dialog.dismiss();
    }
    }
    
    0 讨论(0)
  • 2020-11-21 06:18

    I have another solution for this, and would like to know if it seems valid to you: instead of dismissing in the onDestroy, which seems to be the leading solution, I'm extending ProgressDialog...

    public class MyProgressDialog extends ProgressDialog {
    
      private boolean isDismissed;
    
      public MyProgressDialog(Context context) {
        super(context);
      }
    
      @Override
      public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        dismiss();
      }
    
      @Override
      public void dismiss() {
        if (isDismissed) {
          return;
        }
        try {
          super.dismiss();
        } catch (IllegalArgumentException e) {
          // ignore
        }
        isDismissed = true;
      }
    

    This is preferable, AFAIC, because you don't have to hold the progress dialog as a member, just fire(show) and forget

    0 讨论(0)
  • 2020-11-21 06:19

    I recently faced the same issue.

    The reason behind this issue is that the activity being closed before the dialog is dismissed. There are various reasons for the above to happen. The ones mentioned in the posts above are also correct.

    I got into a situation, because in the thread, I was calling a function which was throwing exception. Because of which the window was being dismissed and hence the exception.

    0 讨论(0)
  • 2020-11-21 06:21

    You can get this exception by just a simple/dumb mistake, by (for example) accidentally calling finish() after having displayed an AlertDialog, if you miss a break call statement in a switch statement...

       @Override
       public void onClick(View v) {
        switch (v.getId()) {
            case R.id.new_button:
                openMyAlertDialog();
                break; <-- If you forget this the finish() method below 
                           will be called while the dialog is showing!
            case R.id.exit_button:
                finish();
                break;
            }
        }
    

    The finish() method will close the Activity, but the AlertDialog is still displaying!

    So when you're staring intently at the code, looking for bad threading issues or complex coding and such, don't lose sight of the forest for the trees. Sometimes it can be just something as simple and dumb as a missing break statement. :)

    0 讨论(0)
  • 2020-11-21 06:22

    I was getting these logs in my video player application. These messages were thrown while the video player was closed. Interestingly, I used to get these logs once in a few runs in a random manner. Also my application does not involve in any progressdialog. Finally, I got around this issue with the below implementation.

    @Override
    protected void onPause()
    {
        Log.v("MediaVideo", "onPause");
        super.onPause();
        this.mVideoView.pause();
        this.mVideoView.setVisibility(View.GONE);
    }
    
    @Override
    protected void onDestroy()
    {
        Log.v("MediaVideo", "onDestroy");
        super.onDestroy();
    }
    
    @Override
    protected void onResume()
    {
        Log.v("MediaVideo", "onResume");
        super.onResume();
        this.mVideoView.resume();
    }
    

    Override the OnPause with call to mVideoView.pause() and the set visibility to GONE. This way I could resolve the "Activity has leaked window" log error issue.

    0 讨论(0)
  • 2020-11-21 06:23

    I had the same obscure error message and had no idea why. Given clues from the previous answers, I changed my non-GUI calls to mDialog.finish() to be mDialog.dismiss() and the errors disappeared. This wasn't affecting my widget's behavior but it was disconcerting and could well have been flagging an important memory leak.

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