我最近一直在研究 Android SDK 平台,不太清楚如何保存应用程序的状态。 对“ Hello,Android”这个例子进行了一些小的改进:
package com.android.hello;
import android.app.Activity;import android.os.Bundle;import android.widget.TextView;
public class HelloAndroid extends Activity {
private TextView mTextView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextView = new TextView(this);
if (savedInstanceState == null) {
mTextView.setText("Welcome to HelloAndroid!");
} else {
mTextView.setText("Welcome back.");
}
setContentView(mTextView);
}}
我认为这对于最简单的情况来说已经足够了,但是它总是以第一条消息作为回应。
我知道写这个就像重写 onPause 或类似的东西一样容易,但是我已经兜兜转转了大约半个小时,也不知道怎么重写。
savedInstanceState只用于保存与当前活动实例相关的状态,例如当前导航或选择信息,这样如果 Android 销毁并重新创建了一个活动,它就可以恢复原状。 请参阅 onCreate "")
和nSaveInstanceState "")
。
要保持延长状态,可以考虑使用 SQLite 数据库、文件或首选项。
需要覆盖 saveinstancestate (Bundle savedInstanceState) ,并写入要更改为 Bundle 参数的应用程序状态值,如下所示:
@Overridepublic 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("MyBoolean", true);
savedInstanceState.putDouble("myDouble", 1.9);
savedInstanceState.putInt("MyInt", 1);
savedInstanceState.putString("MyString", "Welcome back to Android");
// etc.}
Bundle 本质上是一种存储 NVP (“ Name-Value Pair”)映射的方法,它将被传递到 onCreate ()和 onRestoreInstanceState () ,然后在这里提取值,如下所示:
@Overridepublic void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
double myDouble = savedInstanceState.getDouble("myDouble");
int myInt = savedInstanceState.getInt("MyInt");
String myString = savedInstanceState.getString("MyString");}
您通常使用这种技术来存储应用程序的实例值(选择、未保存的文本等)。
请注意,对于可持久化的数据,在 saveinstancestate 和 onstoreinstancestate 上使用是不安全的。
文档(在“活动生命周期”部分) :
请注意,将可持久化的数据保存在onPause()而不是onSaveInstanceState(bundle)中是很重要的,因为后者不是生命周期回调的一部分,所以不会像文档中描述的那样在每种情况下都调用。
换句话说,是将持久数据的保存 / 恢复代码放在 onPause ()和 onResume ()中!
为了进一步澄清,这是 onSaveInstanceState ()文档:
在活动可能被终止之前调用此方法,以便当它在将来某个时间返回时,它可以恢复其状态。 例如,如果活动 b 在活动 a 前面启动,并且在某个时刻活动 a 被去除以回收资源,活动 a 将有机会通过这个方法保存其用户界面的当前状态,以便当用户返回活动 a 时,可以通过 onCreate (Bundle)或 onRestoreInstanceState (Bundle)恢复用户界面的状态。
我的同事写了一篇文章,解释了 Android 设备上的应用状态,包括活动生命周期和状态信息,如何存储状态信息,以及保存到状态 Bundle 和 SharedPreferences。那篇文章介绍了三种方法:
使用实例状态包存储应用程序生命周期(即临时)的本地变量 / ui 控制数据
[Code sample – Store state in state bundle]@Overridepublic void onSaveInstanceState(Bundle savedInstanceState){
// Store UI state to the savedInstanceState.
// This bundle will be passed to onCreate on next call. EditText txtName = (EditText)findViewById(R.id.txtName);
String strName = txtName.getText().toString();
EditText txtEmail = (EditText)findViewById(R.id.txtEmail);
String strEmail = txtEmail.getText().toString();
CheckBox chkTandC = (CheckBox)findViewById(R.id.chkTandC);
boolean blnTandC = chkTandC.isChecked();
savedInstanceState.putString(“Name”, strName);
savedInstanceState.putString(“Email”, strEmail);
savedInstanceState.putBoolean(“TandC”, blnTandC);
super.onSaveInstanceState(savedInstanceState);}
使用共享首选项在应用程序实例(即永久)之间存储局部变量 / ui 控制数据
[Code sample – store state in SharedPreferences]@Overrideprotected void onPause(){
super.onPause();
// Store values between instances here
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit(); // Put the values from the UI
EditText txtName = (EditText)findViewById(R.id.txtName);
String strName = txtName.getText().toString();
EditText txtEmail = (EditText)findViewById(R.id.txtEmail);
String strEmail = txtEmail.getText().toString();
CheckBox chkTandC = (CheckBox)findViewById(R.id.chkTandC);
boolean blnTandC = chkTandC.isChecked();
editor.putString(“Name”, strName); // value to store
editor.putString(“Email”, strEmail); // value to store
editor.putBoolean(“TandC”, blnTandC); // value to store
// Commit to storage
editor.commit();}
使用保留的非配置实例在应用程序生存期内的活动之间在内存中保持对象实例活动
[Code sample – store object instance]private cMyClassType moInstanceOfAClass; // Store the instance of an object@Overridepublic Object onRetainNonConfigurationInstance(){
if (moInstanceOfAClass != null) // Check that the object exists
return(moInstanceOfAClass);
return super.onRetainNonConfigurationInstance();}