简体   繁体   中英

Android: Dismissing popup window in onPause when it's created in a separate method

I have a separate method in my code that creates a popup window:

private void showPopup(final Activity context, int[] buttonLocation) {

    // CREATE POPUP HERE NAMED popup

    // Display popup for 600ms (it's a popup with an animation)
    final Timer t1 = new Timer();
    TimerTask titty1 = new TimerTask(){
        public void run(){
            Romp.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    popup.dismiss();
                    t1.cancel();
                    t1.purge();
                }
            });
        }
    };
    t1.schedule(titty1, 600);
}

When I close my activity elsewhere in the code, and try to go back to the previous activity, I get a "leaked window" error, inferring that I need to dismiss my popup. I dismiss my popup in the timer shown above, however, there's another part of my code that can trigger the activity to end in the mean time.

I want to be able to do something like this in on destroy/pause:

@Override
public void onDestroy() {
    ACTIVITY.this.finish();
    eraseData();
    popup.dismiss();
    super.onDestroy();
}

Is there a way I can dismiss a popup window from onDestroy/pause that was created in a separate method?

call-

titty1.cancel(true);
t1.cacncel();

in your onPause() / onDestroy() according to your use case/requirement.

Update

class Test{
  private final Timer t1 = new Timer();
  private TimerTask titty1=null;
  private void showPopup(final Activity context, int[] buttonLocation) {

    // CREATE POPUP HERE NAMED popup

    // Display popup for 600ms (it's a popup with an animation)
    titty1 = new TimerTask(){
        public void run(){
            Romp.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if(!alive)
                        return;
                    popup.dismiss();
                    t1.cancel();
                    t1.purge();
                }
            });
        }
    };
    t1.schedule(titty1, 600);
  }
  @Override
  public void onDestroy() {

    alive=false;
    ACTIVITY.this.finish();
    eraseData();
    popup.dismiss();
    titty1.cancel(true);
    t1.cancel();
    super.onDestroy();
  }

}

reason for the leaked window is that it is not syncing with the activity it was linked and not closed when the activity closes. So better while accessing the UI elements make sure that the activity is alive.

boolean alive=true;

@Override public void onDestroy() {

    alive=false;
    ACTIVITY.this.finish();
    eraseData();
    popup.dismiss();
    super.onDestroy(); }

private void showPopup(final Activity context, int[] buttonLocation) {

    // CREATE POPUP HERE NAMED popup

    // Display popup for 600ms (it's a popup with an animation)
    final Timer t1 = new Timer();
    TimerTask titty1 = new TimerTask(){
        public void run(){
            Romp.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if(!alive)
                        return;
                    popup.dismiss();
                    t1.cancel();
                    t1.purge();
                }
            });
        }
    };
    t1.schedule(titty1, 600);
}

follow the same before creating the popup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM