简体   繁体   中英

Why isn't onRestoreInstanceState called after onStart?

I am trying to test onRestoreInstanceState method and when (exactly) it is called . So I have followed these steps :

  1. start my activity. onCreate -- > onStart --> onResume were called .
  2. press Home button on the emulator . onPause --> onSaveInstanceState --> onStop were called .
  3. Click the icon in the launcher and launch my activity again. onRestart --> onStart --> onResume were called .

My java code :

package com.test.demostate.app;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;


 public class MainActivity extends ActionBarActivity {
   private int visiters=0;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       Log.d("TAG","onCreate");

   }

  @Override
   protected void onPause() {
      super.onPause();
       Log.d("TAG","onPause");
  }

  @Override
   protected void onStop() {
       super.onStop();
       Log.d("TAG","onStop");
   }

   @Override
   protected void onStart() {
       super.onStart();
       Log.d("TAG","onStart");
   }

   @Override
    protected void onRestart() {
       super.onRestart();
       Log.d("TAG","onRestart");

   }

    @Override
     protected void onResume() {
       super.onResume();
        visiters++;
        Log.d("TAG","onResume");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("visiters",visiters);
        Log.d("TAG",visiters+" visiters  was saved ");
    }

    @Override
     protected void onRestoreInstanceState(Bundle savedInstanceState) {
         super.onRestoreInstanceState(savedInstanceState);
        visiters=savedInstanceState.getInt("visiters");
         Log.d("TAG",visiters+" visiters was restored");
    }

     @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("TAG","onDestroy");
    }
}

From the docs : Instead of restoring the state during onCreate() you may choose to implement onRestoreInstanceState(), which the system calls after the onStart() method .

So onRestoreInstanceState is called

  • after the activity was destroyed onPause --> onStop --> onDestroy then onCreate --> onRestoreInstanceState --> onResume ( due to screen rotation for example )
  • after the activity was stopped onPause --> onStop --> onRestart --> onStart --> onRestoreInstanceState --> onResume ( due to home icon pressing for example )

But why isn't it called after onStart ?

Thanks

After onStart() only if onSaveInstanceState() has been called.

From the docs:

This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle).

This method is called between onStart() and onPostCreate(Bundle).

Activity#onRestoreInstanceState()

The official documentation says about onRestoreInstanceState (Bundle savedInstanceState) :

This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle).

This method is called between onStart() and onPostCreate(Bundle).

When the activity is beign re-initialized?

  • When orientation of the device changes your activity is re-initialized.
  • When there is another activity in front of your app and the OS kills your app for some reason, may be for example free resources.

Try change the orientation of emulator:

Ctrl+F12

Look the answer of user @GAThrawn

Pressing the Home button you leave your app and go to the home screen, whilst leaving your app running in the background. This is a bit like switching between windows on a Windows PC.

Except that when your phone is running low on resources like memory it will start to close apps that are running in the background, so that your phone has enough resources for what you're trying to do now. Games are often amongst the first apps the phone will "kill" to save resources as they often use a lot more memory and CPU than other apps. This is why sometimes your game is still running paused, and sometimes Android has closed it for you.

So I can not prove my second argument, since it decides it is the OS, at least do not know how to prove.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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