Android 返回后在“活动”中保存数据
在此活动中返回后,我想将活动中的数据保存在编辑文本中。怎么做?我是否应该使用暂停和恢复?您可以使用Bundle savedInstanceState保存应用程序的状态。 当我们更改设备的方向时,数据将丢失 在下面的示例中,我们保存该值Android 返回后在“活动”中保存数据,android,Android,在此活动中返回后,我想将活动中的数据保存在编辑文本中。怎么做?我是否应该使用暂停和恢复?您可以使用Bundle savedInstanceState保存应用程序的状态。 当我们更改设备的方向时,数据将丢失 在下面的示例中,我们保存该值 int value = 1; @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); /*
int value = 1;
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
/* EditText editText =
(EditText) findViewById(R.id.editText);
CharSequence text = editText.getText();// getting text in edit text
outState.putCharSequence("savedText", text);// saved text in bundle object (outState)
*/
outState.putInt("value", value);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
/* CharSequence savedText=savedInstanceState.getCharSequence("savedText");
Toast.makeText(this, ""+savedText, Toast.LENGTH_SHORT).show();*/
value = savedInstanceState.getInt("value");
Toast.makeText(this, ""+value, Toast.LENGTH_SHORT).show();
}
public void buttonClicked(View view) {
value++;
}
XML布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:id="@+id/editText"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:onClick="buttonClicked"/>
</LinearLayout>
注意:编辑文本、按钮等中的任何信息
只要您分配了一个ID,Android就会自动保存。
因此,如果在上面的示例中旋转屏幕,第二个编辑文本将清除其信息,但不会编辑id为第一个编辑文本的文本。使用saveinstancestate保存数据,同时在活动之间切换此问题的可能副本有点不清楚。请说得更具体些。