[英]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()
。
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.