سؤال

I've got an "activity a" which reads some values from SharedPreferences and display them in a TextView, then I call "activity b" where the values from SharedPreferences get updated and written back to SharedPreferences. Finally I go back to "activity a" by pressing the back-button, now the new (updated) values should be read from SharedPreferences and shown in the TextView. But here comes the problem, the values just read from SharedPreferences are still not updated (are not the new ones set by activity b) (got it from logcat output), how comes that? Does SharedPrefs need some kind of manual refresh?

If I restart "activity a" everything works just fine and the new values are shown properly. What's the matter?

I call the method to read and show the values from onResume() in "activity a".

I also tried to re-instantiate the SharedPrefs-Object (with getSharedPreferences()) but it doesn't help either.

Thanks in advance!

هل كانت مفيدة؟

المحلول

Are you calling the commit() method in activity b to save the new values.

Eg something like:

SharedPreferences customSharedPreference = getSharedPreferences("abcprefs", 0);
SharedPreferences.Editor editor = customSharedPreference.edit();
editor.putString("key", "val");
editor.commit();

And secondly you can finish() the activity a before being sent to activity b, then from activity b a new instance of activity a will be created and onCreate() will be called.

Alternatively you can refresh the preferences in the onStart() because your activity is probably "no longer visible" when sent to activity b.

See http://developer.android.com/guide/topics/fundamentals/activities.html to see the activity lifecycle.

نصائح أخرى

SharedPreferences is not for sharing data between Activities

Use Intent and Activity.startActivityForResult. See my answer here Get the intent object in an activity

Ensure you're using the same preferences throughout each of your activities: if you're using getSharedPreferences, you should specify the file and level of access. In your case, it sounds like getDefaultSharedPreferences would be the way to go.

Also, make sure that you're not only setting the preferences, but also committing the changes:

SharedPreferences preferences = getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("key", "value");
editor.commit();

and then in your other activities:

SharedPreferences preferences = getDefaultSharedPreferences(this);
boolean myPreference = preferences.getBoolean("key", defaultValue);

This would be easier to help with if you would post the pieces of code in question; if you're still unable to get it working, I would try adding it to your post.

It's also worth noting that preference.edit() returns a different SharedPreferences.Editor each time you call it, so it's important to store the editor into a separate variable, use it to write out the preferences and then commit that editor. E.g. this won't work:

myPrefs.edit().putInt("pref", 1);
myPrefs.edit().putBoolean("pref", true);
myPrefs.edit().commit();

It needs to be (as has been demonstrated):

SharedPreferences myPrefs = getSharedPreferences("pref_name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = myPrefs.edit();
editor.putInt("pref", 1);
editor.putBoolean("pref", true);
editor.commit();

To be able to update your activity A with data sent to the SharedPreferences from activity B while resuming the activity A from B, do the following:

  1. In your app manifest, set the activity A "launchMode" to "standard"

  2. On finishing from the activity B and returning to activity A, add an intent flag of "FLAG_ACTIVITY_CLEAR_TOP" to your intent like below:

    Intent intent = new Intent(activityB.this, activityA.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finish();

To explain the Code: The "FLAG_ACTIVITY_CLEAR_TOP" checks if activity A being started is already running in the current task, then instead of launching the new instance of that Activity, all the other activities on top of it is destroyed and this intent is delivered to the resumed instance of the Activity (now on top), through onNewIntent method. Follow this link to learn more about android task and back stack: https://blog.mindorks.com/android-task-and-back-stack-review-5017f2c18196

Hope this helps...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top