Prevent Activity Stack from being Restored?

后端 未结 5 538
有刺的猬
有刺的猬 2020-12-02 20:48

When an application\'s process is killed, its activity stack is saved. Then when the application is restarted, all my activities resume and run into null pointers. Rather th

5条回答
  •  [愿得一人]
    2020-12-02 21:24

    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);
        }
    }
    

提交回复
热议问题