简体   繁体   English

返回上一个活动的最后状态

[英]Returning back to last state in previous activity

I have two activities A and B. When the application starts, activity A is loaded and I load data to it from database. 我有两个活动A和B。启动应用程序时,活动A被加载,并且我从数据库中加载了数据。 I can go to activity B without any problems, but when I go back to activity A, the old data are viewed for a moment and the activity is reloaded. 我可以没有任何问题地进入活动B,但是当我回到活动A时,会查看一下旧数据,然后重新加载活动。

Any idea how to fix this? 任何想法如何解决这个问题?

在从DB开始加载数据之前,清除所有字段。从加载数据开始花费的时间很少,并且在清除之前的字段之前。

Here You can put all field as blank.. 您可以在此处将所有字段都留为空白。

You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application activity state values you want to change to the Bundle parameter like this: 您需要重写onSaveInstanceState(Bundle savedInstanceState)并将您想要更改的应用程序活动状态值写入Bundle参数,如下所示:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  /* Save UI state changes to the savedInstanceState.
  * This bundle will be passed to onCreate if the process is
  * killed and restarted activity
  */
  savedInstanceState.putString("YourString", "");

  // etc...
  // etc...
}

The Bundle is essentially a way of storing a NVP ("Name-Value Pair") map, and it will get passed in to onCreate() and also onRestoreInstanceState() where you'd extract the values like this: Bundle本质上是一种存储NVP(“名称-值对”)映射的方法,它将被传递给onCreate()以及onRestoreInstanceState(),您可以在其中提取如下值:

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.

  String myString = savedInstanceState.getString("YourString");
}

You would usually use this technique to store instance values for your application (selections, unsaved text) etc. 通常,您将使用此技术来存储应用程序的实例值(选择,未保存的文本)等。

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

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