简体   繁体   中英

How to use onSavedInstance?

One quick question: Why does my onSavedInstance not work? I want to save last state of user activity (current workout session etc.) but in some particular reason it keeps turning me back to the mainActivity when I press the home or overview button and then I return to the application. It should return me the last saved state of activity but something seems to be bugged. I have been struggling with this problem for two weeks. I searched all over the forum but I still can't find the answer. Hope someone can help:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_workout);

    if (savedInstanceState != null) {
        // What should I call here?

    } else {
        // And here?
    }
}


@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
}


@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
}

onSaveInstanceState(Bundle savedInstanceState) is called when the user leaves your app.It provides a Bundle object that you can pass values you want to save as key value pairs.

static final String WORKOUT_STATE = "state";


@Override
public void onSaveInstanceState(Bundle savedInstanceState) {

    // Save the user's current workout state
    savedInstanceState.putInt(WORKOUT_STATE, currentState);


    super.onSaveInstanceState(savedInstanceState);
}

There are two options for where you can restore the current state when the activity is recreated. It can be done in onCreate(Bundle savedInstanceState) method or onRestoreInstanceState(Bundle savedInstanceState). If you should do it in onCreate, you should check if the savedInstanceState is not null.

If savedInstanceState is not null then the activity is being recreated and this is where you extract the values.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        currentState = savedInstanceState.getInt(WORKOUT_STATE);

    } else {

        // Initialize members with default values for a new instance
    }

}

Instead of extracting the values in the onCreate method, you can optionally use the onRestoreInstanceState(Bundle savedInstanceState) method. This method is called if there is a state to restore so you don't need to check if savedInstanceState is null.

public void onRestoreInstanceState(Bundle savedInstanceState) {

    super.onRestoreInstanceState(savedInstanceState);

    // Restore state
    currentScore = savedInstanceState.getInt(WORKOUT_STATE);

}

You can read more about saving UI states here: https://developer.android.com/topic/libraries/architecture/saving-states.html

SOLVED!

I fixed my problem, believe it or not the problem was in FLAG_ACTIVITY_NO_HISTORY in my intent service. Thanks for trying to help!

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