Android 如何将应用程序带到我离开的活动中
假设有人正在使用我的应用程序,他们进入了一个关键状态,比如他们正在输入一些数据。说在同一时间电话铃响,所以他们接电话,迫使我的应用程序进入后台。通话结束后,如果他们点击我的应用程序图标,我希望它能让他们回到他们离开的活动中。我该怎么做 编辑Android 如何将应用程序带到我离开的活动中,android,Android,假设有人正在使用我的应用程序,他们进入了一个关键状态,比如他们正在输入一些数据。说在同一时间电话铃响,所以他们接电话,迫使我的应用程序进入后台。通话结束后,如果他们点击我的应用程序图标,我希望它能让他们回到他们离开的活动中。我该怎么做 编辑 有一件事我忘了澄清(抱歉),那就是用户留下的活动不是主要活动。我需要在通常不是主要入口点的活动中重新打开应用程序。您需要覆盖onSaveInstanceState(Bundle savedInstanceState)并将要保留的状态值保存到Bundle参数,
有一件事我忘了澄清(抱歉),那就是用户留下的活动不是主要活动。我需要在通常不是主要入口点的活动中重新打开应用程序。您需要覆盖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.
savedInstanceState.putBoolean("booleanValue", true);
savedInstanceState.putString("stringValue", "Your String data");
// etc.
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
boolean booleanValue = savedInstanceState.getBoolean("booleanValue");
String stringValue = savedInstanceState.getString("stringValue");
}
Bundle是一种在(“名称-值对”)映射中存储值的方法,它将被传递到onCreate和onRestoreInstanceState,您可以在其中提取如下值:
@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.
savedInstanceState.putBoolean("booleanValue", true);
savedInstanceState.putString("stringValue", "Your String data");
// etc.
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
boolean booleanValue = savedInstanceState.getBoolean("booleanValue");
String stringValue = savedInstanceState.getString("stringValue");
}
您通常使用这种技术来存储应用程序的实例值(选择、未保存的文本等)
现在,您可以用这些值重新填充字段
选中此项:要防止系统启动活动的另一个实例,您应该将
launchMode=“singleTop”
添加到活动中
AndroidManifest.xml
<activity
....
....
android:launchMode="singleTop" />
我认为onpause()中的savedinstancestate会对您有所帮助。否则,在SharedReference中保存当前状态也会对您有所帮助。我忘了强调相关活动不是应用程序的主要活动。我添加了一个编辑。