知道活动是否已被销毁的正确方法 [英] Proper way to know whether an Activity has been destroyed

查看:52
本文介绍了知道活动是否已被销毁的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问知道活动是否被销毁的正确方法是什么?目前,我正在使用以下方式.

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?

推荐答案

这行得通,但是如果在其他应用程序需要优先级的时间内处于非活动状态,则有时操作系统会关闭您的应用程序.可以肯定的是,我知道什么时候变量将被无效,但是不确定在您的情况下是否使用去往主存储器的volatile.但是,确保您获得正确值的一种方法是将其保存在SharedPreferences中.

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. But one way to be sure that you get the right value, is to save it in SharedPreferences.

这篇关于知道活动是否已被销毁的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆