我希望在活动销毁时保存并恢复用户详细信息。我已经搜索了一段时间,但我尝试了代码,但它不起作用。代码看起来正确但它仍然无法正常工作。当我的活动被销毁并且我再次创建它时,它不会显示我保存的内容。 这是XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.gigabytedevelopersinc.savedpractice.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type here"
android:id="@+id/txtone"
android:gravity="center"
android:layout_marginTop="150sp"
android:layout_marginLeft="90sp"
android:freezesText="true"
android:saveEnabled="true"/>
</RelativeLayout>
这是Java
package com.gigabytedevelopersinc.savedpractice;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText firstEdit;
private String storeFirst = "STORE_FIRST";
private String editstore ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstEdit = (EditText)findViewById(R.id.txtone);
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
firstEdit = (EditText)findViewById(R.id.txtone);
outState.putString(storeFirst,firstEdit.getText().toString());
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
firstEdit = (EditText)findViewById(R.id.txtone);
firstEdit.setText(savedInstanceState.getString(storeFirst));
}
}