Home » Android » android – Proper way to know whether an Activity has been destroyed

android – Proper way to know whether an Activity has been destroyed

Posted by: admin May 14, 2020 Leave a comment

Questions:

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?

How to&Answers:

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.