用户关闭应用程序后保存状态,Android
Save state after user kills app, Android
我一直在为我的一些项目而苦苦挣扎。我需要保存 2 个 TextView
值,以便当用户按下后退按钮或重新启动 phone 以及稍后 returns 到应用程序时,它们就在那里。到目前为止,我已经尝试了很多的方法,但不知何故它不会工作......
该应用程序显示 TextView
和 'goal',用户自己输入。第二个 TextView
显示一些 'beams',用户自己再次输入。我已经能够在用户旋转屏幕时保留数据,但在应用程序被杀死后更难保留数据。
public class MainActivity extends AppCompatActivity {
SharedPreferences preferences;
TextView showGoalTextView;
TextView showBeamsTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showGoalTextView = (TextView) findViewById(R.id.textView3);
showBeamsTextView = (TextView) findViewById(R.id.textView2);
preferences = getSharedPreferences("userData", MODE_PRIVATE);
updateGoalTextView();
}
和updateGoalTextView()
方法:
public void updateGoalTextView() {
String goalSave = showGoalTextView.getText().toString();
String beamsSave = showBeamsTextView.getText().toString();
SharedPreferences preferences = getSharedPreferences("userData", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("goals", goalSave);
editor.putString("beam", beamsSave);
editor.apply();
// get the saved string from shared preferences
String name1 = preferences.getString("goals", "");
// set reference to the text view
showGoalTextView = (TextView) findViewById(R.id.textView3);
// set the string from sp as text of the textview
showGoalTextView.setText(name1);
}
updateGoalTextView
在 onCreate
中被调用。希望我使用的方法是正确的,因为如果我 运行 在我的 phone 上使用 AS,它根本不会保存数据并重新创建它。
知道如何解决吗?
要更清楚地了解我的意思,请下载我的测试版应用程序:https://play.google.com/store/apps/details?id=jhpcoenen.connectlife.beams
谢谢所有回答的人。查看答案并在 GOOGLE 上搜索后,我终于解决了它。
您可以使用 SharedPreference
轻松存储 TextView
或 EditText
的值。
如果您想使用自动保存,那么您可以使用 TextWatcher
来获取您的文本类型事件,或者您可以使用 onPause() 和 onResume() 方法来存储和读取值。
这里是在 SharedPreferences
.
中存储数据的示例代码
初始化SharedPreferences
-
SharedPreferences pref=getSharedPreferences("my_shared_preferences",MODE_PRIVATE);
将数据存储在SharedPreferences
-
SharedPreferences.Editor editor=pref.edit();
editor.putString("key1","value 1");
editor.putString("key2","value 2");
editor.commit();
从SharedPreferences
-
读取数据
String var1=pref.getString("key1","")
String var2=pref.getString("key2","")
希望它能帮助您找到解决方案。谢谢。
我一直在为我的一些项目而苦苦挣扎。我需要保存 2 个 TextView
值,以便当用户按下后退按钮或重新启动 phone 以及稍后 returns 到应用程序时,它们就在那里。到目前为止,我已经尝试了很多的方法,但不知何故它不会工作......
该应用程序显示 TextView
和 'goal',用户自己输入。第二个 TextView
显示一些 'beams',用户自己再次输入。我已经能够在用户旋转屏幕时保留数据,但在应用程序被杀死后更难保留数据。
public class MainActivity extends AppCompatActivity {
SharedPreferences preferences;
TextView showGoalTextView;
TextView showBeamsTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showGoalTextView = (TextView) findViewById(R.id.textView3);
showBeamsTextView = (TextView) findViewById(R.id.textView2);
preferences = getSharedPreferences("userData", MODE_PRIVATE);
updateGoalTextView();
}
和updateGoalTextView()
方法:
public void updateGoalTextView() {
String goalSave = showGoalTextView.getText().toString();
String beamsSave = showBeamsTextView.getText().toString();
SharedPreferences preferences = getSharedPreferences("userData", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("goals", goalSave);
editor.putString("beam", beamsSave);
editor.apply();
// get the saved string from shared preferences
String name1 = preferences.getString("goals", "");
// set reference to the text view
showGoalTextView = (TextView) findViewById(R.id.textView3);
// set the string from sp as text of the textview
showGoalTextView.setText(name1);
}
updateGoalTextView
在 onCreate
中被调用。希望我使用的方法是正确的,因为如果我 运行 在我的 phone 上使用 AS,它根本不会保存数据并重新创建它。
知道如何解决吗? 要更清楚地了解我的意思,请下载我的测试版应用程序:https://play.google.com/store/apps/details?id=jhpcoenen.connectlife.beams
谢谢所有回答的人。查看答案并在 GOOGLE 上搜索后,我终于解决了它。
您可以使用 SharedPreference
轻松存储 TextView
或 EditText
的值。
如果您想使用自动保存,那么您可以使用 TextWatcher
来获取您的文本类型事件,或者您可以使用 onPause() 和 onResume() 方法来存储和读取值。
这里是在 SharedPreferences
.
初始化SharedPreferences
-
SharedPreferences pref=getSharedPreferences("my_shared_preferences",MODE_PRIVATE);
将数据存储在SharedPreferences
-
SharedPreferences.Editor editor=pref.edit();
editor.putString("key1","value 1");
editor.putString("key2","value 2");
editor.commit();
从SharedPreferences
-
String var1=pref.getString("key1","")
String var2=pref.getString("key2","")
希望它能帮助您找到解决方案。谢谢。