Android:如何在 activity A 中保存 sharedPreferences 并在 activity B 中访问它
Android: How to save sharedPreferences in activity A and access it in activity B
我看到之前有人问过这个问题,但是 none 提供的答案对我有用。
所以,我有两个活动:A 和 B。用户在 A 中插入一些数据,我希望能够从 B 访问它们(以及未来的更多活动)。我正在尝试使用共享首选项来做到这一点。现在看来我的代码能够正确地将数据保存在 activity A 中,但是我无法从 activity B 访问与对象相同的 sharedPreference(在 activity B 中) 是空的。看起来它创建了另一个同名对象。
我是 android 和 java 的新手,所以我知道可能只是我不明白 class 是如何工作的,我做错了什么?
Activity一个
SharedPreferences sharedPref = this.getSharedPreferences("PREF_PERSONAL_DATA",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.n),n);
editor.putInt(getString(R.string.hi), hi);
editor.putInt(getString(R.string.wa), wa);
editor.putInt(getString(R.string.he), he);
editor.putInt(getString(R.string.we), we);
editor.putInt(BlocksNumStr, BlocksNum);
editor.commit();
Activity B
SharedPreferences sharedPref = this.getSharedPreferences("PREF_PERSONAL_DATA", Context.MODE_PRIVATE);
String Weight = sharedPref.getString("we", null);
int W = sharedPref.getInt("weight", 0);
TextView ShowWeight = (TextView) findViewById(R.id.attempt);
ShowWeight.setText(Weight);
TextView ShowW = (TextView) findViewById(R.id.attemptW);
ShowW.setText(W);
A
正在存储一个 int
值,但 B
正试图将其作为 String
检索——这应该导致 SharedPreferences.getString() 抛出一个ClassCastException
。我推测您的第二个代码片段被捕获此异常的 try/catch 包围。
您必须使用 SharedPreferences.getInt() 检索 int
。
确保您使用相同共享首选项实例的最安全方法是调用
getDefaultSharedPreferences(Context context)
首先将这些添加到您的两个 Activties A 和 B
private SharedPreference mSharedPreference;
私人 SharedPreference.Editor mEditor;
像这样在 onCreate 中启动它们:
mSharedPreference = PreferenceManager.getDefaultSharedPreferences(this);
mEditor = mSharedPreference.edit();
要保存到 sharedPreference,请执行以下操作:
mEditor.put("KEY",variableToSave);
mEditor.apply();
阅读 (activity B):
//读取以前保存的字符串(例如...)
String readVariable = mSharedPreferences.getString("KEY","DEAULT_VALUE");
使用默认的共享首选项将使您能够访问整个应用程序中的文件。因此,您可以在任何地方(在 activity A 或 B)中访问使用编辑器保存的变量;
activity答:
SharedPreferences sharedPref = getBaseContext().getSharedPreferences("PREF_PERSONAL_DATA",getBaseContext().MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.n),n);
editor.putInt(getString(R.string.hi), hi);
editor.putInt(getString(R.string.wa), wa);
editor.putInt(getString(R.string.he), he);
editor.putInt(getString(R.string.we), we);
editor.putInt(BlocksNumStr, BlocksNum);
editor.commit();
activity乙:
SharedPreferences sharedPref = getBaseContext().getSharedPreferences("PREF_PERSONAL_DATA", getBaseContext().MODE_PRIVATE);
String Weight = sharedPref.getString(getString(R.string.wa), null);
int W = sharedPref.getInt("weight", 0);
TextView ShowWeight = (TextView) findViewById(R.id.attempt);
ShowWeight.setText(Weight);
TextView ShowW = (TextView) findViewById(R.id.attemptW);
ShowW.setText(W);
Android 有 3 种访问方式 SharedPreferences
:
Activity.getPreferences()
- 访问 Activity-specific 首选项。在您的情况下,这仅对 ActivityA 或 ActivityB 有效。
Activity.getSharedPreferences()
或 Context.getSharedPreferences
当不在 Activity 中时 - 访问 Application-level 首选项。这些首选项在您的应用程序中随处可见。
PreferenceManager.getDefaultSharedPreferences()
- 访问安装在 Android. 上的每个应用程序可见的全局共享首选项
从您发布的示例来看,一切似乎都是正确的,因为您使用了 getSharedPreferences()
。我要检查的内容是:
- 确保是否使用来自资源的相同
String
键来检索首选项。
- 检查
commit()
方法是否不中断写入批首选项。就个人而言,我在使用 commit()
方法时遇到了一些偏好没有被存储的问题。来自 Android Honeycomb 有一个 apply()
方法。 commit()
和 apply()
之间的区别在于 commit()
保存您在任何线程上的首选项,而 apply()
异步工作。如果您不依赖 pre-Honeycomb 版本,请考虑使用 apply()
.
我看到之前有人问过这个问题,但是 none 提供的答案对我有用。
所以,我有两个活动:A 和 B。用户在 A 中插入一些数据,我希望能够从 B 访问它们(以及未来的更多活动)。我正在尝试使用共享首选项来做到这一点。现在看来我的代码能够正确地将数据保存在 activity A 中,但是我无法从 activity B 访问与对象相同的 sharedPreference(在 activity B 中) 是空的。看起来它创建了另一个同名对象。
我是 android 和 java 的新手,所以我知道可能只是我不明白 class 是如何工作的,我做错了什么?
Activity一个
SharedPreferences sharedPref = this.getSharedPreferences("PREF_PERSONAL_DATA",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.n),n);
editor.putInt(getString(R.string.hi), hi);
editor.putInt(getString(R.string.wa), wa);
editor.putInt(getString(R.string.he), he);
editor.putInt(getString(R.string.we), we);
editor.putInt(BlocksNumStr, BlocksNum);
editor.commit();
Activity B
SharedPreferences sharedPref = this.getSharedPreferences("PREF_PERSONAL_DATA", Context.MODE_PRIVATE);
String Weight = sharedPref.getString("we", null);
int W = sharedPref.getInt("weight", 0);
TextView ShowWeight = (TextView) findViewById(R.id.attempt);
ShowWeight.setText(Weight);
TextView ShowW = (TextView) findViewById(R.id.attemptW);
ShowW.setText(W);
A
正在存储一个 int
值,但 B
正试图将其作为 String
检索——这应该导致 SharedPreferences.getString() 抛出一个ClassCastException
。我推测您的第二个代码片段被捕获此异常的 try/catch 包围。
您必须使用 SharedPreferences.getInt() 检索 int
。
确保您使用相同共享首选项实例的最安全方法是调用
getDefaultSharedPreferences(Context context)
首先将这些添加到您的两个 Activties A 和 B
private SharedPreference mSharedPreference; 私人 SharedPreference.Editor mEditor;
像这样在 onCreate 中启动它们:
mSharedPreference = PreferenceManager.getDefaultSharedPreferences(this);
mEditor = mSharedPreference.edit();
要保存到 sharedPreference,请执行以下操作:
mEditor.put("KEY",variableToSave);
mEditor.apply();
阅读 (activity B): //读取以前保存的字符串(例如...)
String readVariable = mSharedPreferences.getString("KEY","DEAULT_VALUE");
使用默认的共享首选项将使您能够访问整个应用程序中的文件。因此,您可以在任何地方(在 activity A 或 B)中访问使用编辑器保存的变量;
activity答:
SharedPreferences sharedPref = getBaseContext().getSharedPreferences("PREF_PERSONAL_DATA",getBaseContext().MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.n),n);
editor.putInt(getString(R.string.hi), hi);
editor.putInt(getString(R.string.wa), wa);
editor.putInt(getString(R.string.he), he);
editor.putInt(getString(R.string.we), we);
editor.putInt(BlocksNumStr, BlocksNum);
editor.commit();
activity乙:
SharedPreferences sharedPref = getBaseContext().getSharedPreferences("PREF_PERSONAL_DATA", getBaseContext().MODE_PRIVATE);
String Weight = sharedPref.getString(getString(R.string.wa), null);
int W = sharedPref.getInt("weight", 0);
TextView ShowWeight = (TextView) findViewById(R.id.attempt);
ShowWeight.setText(Weight);
TextView ShowW = (TextView) findViewById(R.id.attemptW);
ShowW.setText(W);
Android 有 3 种访问方式 SharedPreferences
:
Activity.getPreferences()
- 访问 Activity-specific 首选项。在您的情况下,这仅对 ActivityA 或 ActivityB 有效。Activity.getSharedPreferences()
或Context.getSharedPreferences
当不在 Activity 中时 - 访问 Application-level 首选项。这些首选项在您的应用程序中随处可见。PreferenceManager.getDefaultSharedPreferences()
- 访问安装在 Android. 上的每个应用程序可见的全局共享首选项
从您发布的示例来看,一切似乎都是正确的,因为您使用了 getSharedPreferences()
。我要检查的内容是:
- 确保是否使用来自资源的相同
String
键来检索首选项。 - 检查
commit()
方法是否不中断写入批首选项。就个人而言,我在使用commit()
方法时遇到了一些偏好没有被存储的问题。来自 Android Honeycomb 有一个apply()
方法。commit()
和apply()
之间的区别在于commit()
保存您在任何线程上的首选项,而apply()
异步工作。如果您不依赖 pre-Honeycomb 版本,请考虑使用apply()
.