Skip to content Skip to sidebar Skip to footer

Android: Updating Sharedprefereces On Activity Resume Doesn't Work

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

Solution 1:

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

Eg something like:

SharedPreferencescustomSharedPreference= getSharedPreferences("abcprefs", 0);
SharedPreferences.Editoreditor= 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.

Solution 2:

SharedPreferences is not for sharing data between Activities

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

Solution 3:

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:

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

and then in your other activities:

SharedPreferencespreferences= getDefaultSharedPreferences(this);
booleanmyPreference= 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.

Solution 4:

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):

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

Solution 5:

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...

Post a Comment for "Android: Updating Sharedprefereces On Activity Resume Doesn't Work"