简体   繁体   English

Android:使用单独的方法创建onPause时关闭其弹出窗口

[英]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? 有没有办法可以消除在单独方法中创建的onDestroy / pause弹出窗口?

call- 呼叫-

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

in your onPause() / onDestroy() according to your use case/requirement. 根据您的用例/需求在onPause() / onDestroy()

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. 因此,在访问UI元素时最好确保活动处于活动状态。

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. 创建弹出窗口之前,请遵循相同的步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:创建的弹出窗口未在设备上清晰显示 - Android:Created popup window is not displaying clearly on the device 有没有办法检测弹出窗口何时出现在 Android 中 - Is there a way to detect when a popup window appears in Android 在Android上开发自定义摄像头时,实现onPause,onResume,surfaceCreated和surfaceDestroyed的正确方法是什么? - What is the proper method of implementing onPause, onResume, surfaceCreated and surfaceDestroyed when developing a custom camera on Android? 如何在 onPause 中禁用 onCreate 中创建的 On Click Listener? (在 Android 工作室中) - How to disable On Click Listener created in onCreate, in onPause? (In Android Studio) Android - 带弹出窗口的NullPointerException - Android - NullPointerException with popup window Android-onPause,onCreate,setContentView不起作用-未定义类型myMain的方法X - Android - onPause, onCreate, setContentView not working - method X is undefined for the type myMain 维护Android Activity的数据:onPause,onSaveInstanceState,onRetainNonConfigurationInstance - Maintaining Android Activity's data: onPause, onSaveInstanceState, onRetainNonConfigurationInstance 弹出窗口InputEventReceiver错误-Android - Popup Window InputEventReceiver Error - Android Android,在弹出窗口中的 TextView 中设置文本 - Android, setText in TextView in popup window 弹出窗口在画布Android上 - popup window over canvas Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM