我想存储一个字符串值,甚至需要检索应用程序是否重新打开并将其传递给另一个要在该活动中使用的活动。有人可以通过示例代码/项目指导我吗
答案 0 :(得分:3)
使用开发人员Android文档 here 中提供的共享首选项。更多适应症here
如Android文档中所述:
final String KEY = "stringKey";
String myString = "someStringContent";
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); // this = your Context
SharedPreferences.Editor editor = settings.edit();
editor.putString(KEY, myString);
// Apply the edits! This happens asynchronously
editor.apply();
之后,要在重新打开应用时检索该值:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
String myString = settings.getString(KEY, "default value");