Reputation: 11
I have two activities. In the first activity, I have a SharedPreferences with a TextView, which displays the amount of data in the SharedPreferences. These codes are written in the onCreate()
SharedPreferences coin = getSharedPreferences("saved", MODE_PRIVATE);
SharedPreferences.Editors editor2 = coin.edit();
editor2.putInt("t", 0).apply();
editor.commit();
TextView name = (TextView) findViewById(R.id.textview);
Integer a = coin.getInt("t", 0);
name.setText(a.toString());
In the second activity, I wanna add 10 points to the data and update the SharedPreferences. These codes are not inside onCreate().
SharedPreferences coin = getSharedPreferences("saved", MODE_PRIVATE);
SharedPreferences.Editors editor2 = coin.edit();
int a = coin.getInt("t", 0);
int b = a + 10;
editor2.putInt("t", Integer.valueOf(b));
The problem is, after adding 10 units there is no update in the TextView so I don't even know if the 10 units have been added correctly to SharedPreferences or not.
Please help
Upvotes: 0
Views: 1122
Reputation: 1071
You have to register OnSharedPreferenceChangeListener
in your first activity in order to listen to the changes and then update your TextView with the updated value.
Implement OnSharedPreferenceChangeListener
in your FirstActivity:
public class FirstActivity extends AppCompatActivity implements
SharedPreferences.OnSharedPreferenceChangeListener {
...
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
// This method will be called when there is a change in SharedPreference
// Update your textview here
if(s.equals("t")) {
Integer a = sharedPreferences.getInt("t", 0);
name.setText(a.toString());
}
}
}
In your FirstActivity's onStart()
and onStop()
method, manage the listener:
@Override
protected void onStart() {
super.onStart();
getSharedPreferences("saved", MODE_PRIVATE).registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onStop() {
getSharedPreferences("saved", MODE_PRIVATE).unregisterOnSharedPreferenceChangeListener(this);
super.onStop();
}
But remember, once you leave your FirstActivity, the listener gets unregistered and you will no longer receive updates. To reflect the updates even after the activity go background, you can just update your textview in your onStart()
so that when the FirstActivity starts, the textview gets updated with the latest value from SharedPreferences:
@Override
protected void onStart() {
super.onStart();
SharedPreferences coin = getSharedPreferences("saved", MODE_PRIVATE);
coin.registerOnSharedPreferenceChangeListener(this);
// Also Update the textview here
Integer a = coin.getInt("t", 0);
name.setText(a.toString());
}
Now, any update to the preference from anywhere will also change the textview text as soon as the FirstActivity comes into foreground or launches.
Upvotes: 0
Reputation: 67
If I understand, you would like reflect the final value of t
in SharedPreference
in your first activity after its updated in second activity?
To do that, create a function updateNameTV()
which would contain logic to update the TextView with the value of t
from SharedPreference
and call that function in onResume()
of the FIRST activity like:
@Override
protected void onResume() {
// TODO Auto-generated method stub
updateNameTV();
super.onResume();
}
So when Activity 2 is launched, Activity 1 goes in back of the stack and onPause()
gets called.. when we return to Activity 1 this activity is pop back and onStart()
followed by onResume()
is called.
These are the lifecycle events more here
So the plan is, when onResume()
is called we update the TextView value with the latest SharedPreference
And we do that before calling super.onResume()
such that update is performed before handing over the control to next lifecycle event. Hope this gives more clarity.
Upvotes: 2