简体   繁体   English

正确的方法来了解活动是否已被销毁

[英]Proper way to know whether an Activity has been destroyed

May I know what is the proper way to know whether an Activity has been destroyed? 我可以知道知道某项活动是否已被销毁的正确方法是什么? Currently, I am using the following way. 目前,我使用以下方式。

private volatile boolean isOnDestroyCalled = false;

@Override
protected void onDestroy() {
    super.onDestroy();
    isOnDestroyCalled = true;
}

public boolean isOnDestroyCalled() {
    return this.isOnDestroyCalled;
}

Is there any other way better than the above? 有没有比上面更好的方法?

That will work, but sometimes the OS will go and shut your application down if it's inactive an amount of time when other applications need priority. 这样可行,但有时候操作系统会关闭你的应用程序,如果它在其他应用程序需要优先级时处于非活动状态的时间。 For sure I know when that happens the variables will get nullified, not sure though if it would in your case using volatile which goes to main memory. 当然,我知道当发生这种情况时,变量将被取消,但不确定是否会在你的情况下使用volatile进入主内存。 But one way to be sure that you get the right value, is to save it in SharedPreferences. 但是,确保获得正确值的一种方法是将其保存在SharedPreferences中。

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

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