Android Value of SharedPreferences is not updating while app is running

Viewed 306

I have 2 activity say for e.g MainActivity and PlayActivity. I am retrieving value of currentLevel from SharedPreferences in MainActivity using following code.

sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
     // comment this to see if value is saved or not
    currentLevel=sharedPreferences.getInt("currentlevel",1);

and I am Updating value of currentLevel in PlayActivity using following code.

sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("currentlevel",currentLevel+1);
    editor.apply();

Now my problem is while running app i am not able to update its value. For e.g while i am in PlayActivity and i won a level and i updated value of Shared Preference but while running app if i go back to MainActivity and try to retrieve CurrentLevel value, i got previous value not updated one. Please Help. One more thing if i push updated of my app in future will sharedPrefrence also get updated with default 1 value or it will retain its previous value.

2 Answers
sharedPreferences= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("currentlevel",currentLevel+1);
    editor.commit();

I have created a quick sample for you to inspect. It's a "Empty Activity" project (as per the latest Android Studio template) with all defaults (in Kotlin).

After the project was created, I added a second Empty Activity (via the same template), and then I modified their layouts to have an Edit Text and a button.

What do Activities do when they start?

  1. Load any existing Shared Preference value.
  2. Populate the EditText with the existing value (which defaults to 0)

What does button click do?

  1. Save existing value into shared preferences.
  2. Launches next (or previous) activity.

Activity 1 goes to Activity 2, and Activity 2 goes back to 1.

Notice how no matter what number and what activity you use, the other picks it right away.

Here's where I left the project for your inspection:

https://github.com/Gryzor/MySharedPreferences

If you don't want to bother with a git clone, here's what Activity 1 does:

        // Obtain our private Shared Preferences 
        // This comes from ContextWrapper.
        val sharedPreferences = getSharedPreferences("sample", Context.MODE_PRIVATE)

        // restore existing value
        val current = sharedPreferences.getInt("A_NUMBER", 0)
        editText.setText("$current")

        button.setOnClickListener {
            val text = editText.text.toString()

            val value = try {
                text.toInt()
            } catch (e: NumberFormatException) {
                0
            }

            // Save the current value
            sharedPreferences.edit().putInt("A_NUMBER", value).apply()

            // ... and go to the next activity
            val intent = Intent(this, SecondActivity::class.java)
            startActivity(intent)
            finish()
        }

The other activity is identical, sans the intent line:

val intent = Intent(this, MainActivity::class.java)

Conclusion

What I wanted to point out with all this nonsense, is that you are mismanaging your instances and not correctly using something. It's hard to tell because we haven't seen your source code.

Good luck :-)

Related