What is savedInstanceState?

2

I'm new to android and I want to learn more so I'm in github seeing some projects ready and I would like to know what this is:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tempo_agora_main);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}

This savedInstanceState what does this do? By the way, do you explain what this method does?

    
asked by anonymous 28.11.2016 / 20:03

1 answer

12

savedInstanceState is a parameter of the onCreate () that receives an argument of type Bundle .

It is used by the system to, when re-creating an Activity, allow restore status that it had at the time it was destroyed, for example, because the user has rotated the device.

Understand that status here refers only to the content of the views of the Activity layout, ie the system only automatically saves the state of the layout. Any other information that Activity has will be lost, unless it is explicitly stored in that Bundle.

To do this, Activity provides the method onSaveInstanceState () that is called before the Activity can be destroyed. The system gives it the Bundle object that is later retrieved in the onCreate() method when the Activity is recreated, allowing another type of information to be saved and then retrieved.

Example of documentation where you save the value of the variables mCurrentScore and mCurrentLevel :

static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

which are then retrieved in onCreate() , if Activity has been recreated:

...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

In the code example that you put in the question the savedInstanceState is only being used to check if the onCreate() is being called due to a recreation of the Activity or not.

If savedInstanceState == null is because the Activity is being created for the first time, in this case the PlaceholderFragment is created and placed in R.id.container .

    
29.11.2016 / 00:13