Prevent Activity Stack from being Restored? Prevent Activity Stack from being Restored? android android

Prevent Activity Stack from being Restored?


The only solution I was able to find was to check a global static variable in every instance of onCreate() and finish if that variable had been reset to null, indicating the task had been restarted. I close all activities down to my root activity and start over.

Soon I hope to have my app at a point where it can save needed values in onPause(), but 'til then this is the only reliable way I know to work with lost initialization...


If you're working with a spalshScreen or whatever LauncherActivity, you can create a Global static boolean and use it like this :

first find a way to store this static variable (maybe create a Global java file)

public abstract class Global {    ...    public static boolean processKilled = true;    ...}

then, in your laucherActivity (MainActivity or Splashscreen ...) add these lines :

@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        Global.processKilled = false;        ...//your code here    }

In fact, if your app process died it surely wont pass through the code of your launcherActivity. So the static boolean processKilled will remain true. Even if it does, that means your app is currently restarting and processkiled will be correclty set to true and all variables correclty instantiated (No NullPointerException)

By creating your own restartApp method you'll get what you want :

(in every activity you have add these lines :)

@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        if (Global.processKilled){            restartApp();        }        ...//your code here    }

EDIT

if you're not a global variable-aholic, you may want to check if savedInstanceState is or isn't null...


I use this piece of code:

public class NoRestoreActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        // Shoul be always NULL when created the first time, just in case...        if (savedInstanceState != null && savedInstanceState.getInt("SavedInstance") > 0) {            // ----- Your prefferred way to kill an application ----            try {                                this.finishActivity(0);                           } catch (Exception ee) {            }            try {                android.os.Process.killProcess(android.os.Process.myPid());                System.exit(10);            } catch (Exception eeee) {            }            return;        }        super.onCreate(savedInstanceState);    }    @Override    protected void onSaveInstanceState (Bundle outState){        super.onSaveInstanceState(outState);        outState.putInt("SavedInstance",1);    }}