简体   繁体   English

如何使用 onSavedInstance?

[英]How to use onSavedInstance?

One quick question: Why does my onSavedInstance not work?一个简单的问题:为什么我的 onSavedInstance 不起作用? 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.我想保存用户活动的最后状态(当前的锻炼会话等),但由于某种特殊原因,当我按下主页概览按钮然后返回应用程序时,它不断将我转回 mainActivity。 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. onSaveInstanceState(Bundle savedInstanceState) 在用户离开您的应用程序时被调用。它提供了一个 Bundle 对象,您可以传递要保存为键值对的值。

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).它可以在 onCreate(Bundle savedInstanceState) 方法或 onRestoreInstanceState(Bundle savedInstanceState) 中完成。 If you should do it in onCreate, you should check if the savedInstanceState is not null.如果您应该在 onCreate 中执行此操作,您应该检查 savedInstanceState 是否不为 null。

If savedInstanceState is not null then the activity is being recreated and this is where you extract the values.如果savedInstanceState 不为空,则正在重新创建活动,这是您提取值的地方。

@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.您可以选择使用 onRestoreInstanceState(Bundle savedInstanceState) 方法,而不是在 onCreate 方法中提取值。 This method is called if there is a state to restore so you don't need to check if savedInstanceState is null.如果有要恢复的状态,则调用此方法,因此您无需检查 savedInstanceState 是否为 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您可以在此处阅读有关保存 UI 状态的更多信息: 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.我解决了我的问题,不管你信不信,问题出在我的意图服务中的FLAG_ACTIVITY_NO_HISTORY中。 Thanks for trying to help!感谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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