简体   繁体   English

Android - 在不同活动之间切换时保存当前 onCreate state

[英]Android - Save current onCreate state when switching between different activities

I am new to java and android studio and after reading documentation I am still a little confused with the activity lifecycle and how to use it effectively.我是 java 和 android 工作室的新手,在阅读文档后,我仍然对活动生命周期以及如何有效使用它感到有些困惑。

I am building an application which has several activities.我正在构建一个具有多个活动的应用程序。 There is key data loaded in when the app is first opened in the main activity.首次在主活动中打开应用程序时会加载关键数据。 However when I move to another activity and then back to the main, the onCreate is invoked again which I do not want.但是,当我移动到另一个活动然后回到主活动时,再次调用我不想要的 onCreate。 I am aware there is a way to save the instance of your onCreate but I am still unsure of how to do this.我知道有一种方法可以保存您的 onCreate 实例,但我仍然不确定如何执行此操作。 Here is what I have done:这是我所做的:

 @Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    if (outState != null) {
        // Restore value of members from saved state
        outState.putString("step_count", String.valueOf(stepCount));
        outState.putString("cal_count", String.valueOf(calCount));
        outState.putString("dis_count", String.valueOf(disCount));
        android.util.Log.d(TAG, "onSaveInstanceState");

    } else {
        // Probably initialize members with default values for a new instance
    }

}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    savedInstanceState.getString("step_count", String.valueOf(stepCount));
    savedInstanceState.getString("cal_count", String.valueOf(calCount));
    savedInstanceState.getString("dis_count", String.valueOf(disCount));
    stepCount.setText(String.valueOf(stepCount));
    calCount.setText(String.valueOf(calCount));
    disCount.setText(String.valueOf(disCount));
    android.util.Log.d(TAG, "onRestoreInstanceState");

}

Would appreciate any sort of support and guidance.将不胜感激任何形式的支持和指导。 Thank you.谢谢你。

Again - not a simple question.再次 - 这不是一个简单的问题。

First of all: Yes, you can save data "from last session" by overriding: onSaveInstanceState() and restore it overriding onRestoreInstanceState(savedInstanceState: Bundle?).首先:是的,您可以通过覆盖“从上次会话”保存数据:onSaveInstanceState() 并通过覆盖 onRestoreInstanceState(savedInstanceState: Bundle?) 恢复它。 Look here: https://developer.android.com/guide/components/activities/activity-lifecycle#saras看这里: https://developer.android.com/guide/components/activities/activity-lifecycle#saras

Second of all: According to your description it seems that you need SharedPreferences more than this.其次:根据您的描述,您似乎需要 SharedPreferences 比这更多。 If you need a key to anything and it should be used from opening of the app until it is killed or user is logged out, SharedPreferences are the most common solution.如果您需要任何东西的密钥,并且应该从打开应用程序直到它被杀死或用户注销,SharedPreferences 是最常见的解决方案。

onSaveinstanceState() was created to make possibility to remember what was on the screen "in the last session" without "calculating" it again.创建 onSaveinstanceState() 是为了能够记住“在最后一个会话中”屏幕上的内容,而无需再次“计算”它。 For example EditText.text or ImageView.url or similar.例如 EditText.text 或 ImageView.url 或类似的。

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

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