Hi for me one doubt if I change any content or color of activity example, in activity1 I have one textview with data "apple" from shared preference. And I am clicking a button to start new activity2 there I am clicking one button to update the shared preference "apple" to "gova" I click back button of activity2 textview in activity should update. Is there any way to update without onResume() method or explain how onResume() working...?
Asked
Active
Viewed 960 times
-2
-
use `startActivityForResult()` and update values in `onActivityResult`. Please see http://stackoverflow.com/a/10407371/1979347 – Rohan Kandwal Oct 20 '15 at 10:48
-
Please read [the official documentation about Android's activity lifecycle](http://developer.android.com/training/basics/activity-lifecycle/pausing.html), and unless you want to complicate yourself with `startActivityForResult()` (it's good if it's used correctly), please use `onResume()` instead. – Andrew T. Oct 20 '15 at 10:59
-
Hi same another one problem i have one textview with green colour. After clicking one button i m changing textview colour to red. And i m minimizing that app and again opening from recent from mobile that textview colour showing as green colour. How....? – Sureshkumar S Oct 20 '15 at 11:10
3 Answers
0
onResume() is called every time your activity is shown to the user. Thus, if the user goes to another activity and then comes back to the first one, onResume() will be called. Your code should look like:
@Override
protected void onResume() {
super.onResume();
myTextView.setText(preferences.getString("myKey", "defaultValue");
}
If, for any reason, you'd prefer not to use onResume(), you can register a OnSharedPreferenceChangeListener, which will be called every time a shared pref value is changed.
Your first activity will include code that looks like this:
public class MainActivity extends AppCompatActivity {
SharedPreferences preferences;
SharedPreferences.OnSharedPreferenceChangeListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("myKey")) {
// update your textview
}
}
};
preferences.registerOnSharedPreferenceChangeListener(listener);
}
@Override
protected void onDestroy() {
super.onDestroy();
preferences.unregisterOnSharedPreferenceChangeListener(listener);
}
}

Andrea
- 351
- 1
- 7
-
While it's correct that `OnSharedPreferenceChangeListener` will be called every time the shared pref's value is changed, how to update the text on Activity 1's `TextView` using it? – Andrew T. Oct 20 '15 at 11:04
-
@AndrewT. what's the problem? The `OnSharedPreferenceChangeListener` is declared and registered in the first activity. From the point of view of the first activity, we don't care what happens in other activities, we just know that if the shared pref's value changes we update the `TextView` accordingly. – Andrea Oct 20 '15 at 11:09
-
No problem, since I find it's a bit unusual, and I just knew this way from your answer. I haven't tested the code, but will it work, given that `Activity1` is not on foreground? – Andrew T. Oct 20 '15 at 11:12
-
@AndrewT. I would think so, but I'm not entirely sure and I can't test it right now. I guess the best option still remains to update the `TextView` in onResume, I've updated the answer with an example. – Andrea Oct 20 '15 at 11:41
-1
Just override onBackPressed
method in second activity and in it set the value to shared preferences :
@Override
public void onBackPressed() {
// Set shared preference value
}

SANAT
- 8,489
- 55
- 66
-
Please read the question carefully. Changing the shared preference value **doesn't** automatically update the `TextView`'s text. – Andrew T. Oct 20 '15 at 11:07
-2
You should ideally use onBackPressed instead on onResume. Below is a code snippet on similar lines that should help
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setTitle("Really go back?")
.setMessage("Are you sure you want to?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Activity1.super.onBackPressed();
//change sharedPreferences here
}
}).create().show();
}
Hope this helps

BountyHunter
- 1,413
- 21
- 35
-
This answer isn't correct, what if user hasn't updated the value? I don't understand the use of `onBackPressed()` in this scenario. – Rohan Kandwal Oct 20 '15 at 10:49
-
An IF statement can always be included inside this call to check if the sharedPreferences value is equal to the current selection. – BountyHunter Oct 20 '15 at 10:52
-
This function will come in second activity won't it? How will it update "apple" to "gova" in the first activity from this code? – Rohan Kandwal Oct 20 '15 at 10:55
-
1This is not correct @ BountyHunter how atctivity2's onBackPress will update the data of activity1 – Sureshkumar S Oct 20 '15 at 10:56
-
Data for textview in activity1 is coming from the sharedPreferences. So if I update the sharedPreference while leaving activity2, it should update it, provided there is onRestart() in activity one. My bad I missed the second part – BountyHunter Oct 20 '15 at 11:00
-
@BountyHunter What you are really doing is showing a confirmation dialog to ask a user if he wants to go back and if he says yes then saving changes. How is this any different from what user has already tried? You just added a dialog on back button pressed in second activity. – Rohan Kandwal Oct 20 '15 at 11:03
-
In second activity while clicking button i m updating shared preference then why onBackPress – Sureshkumar S Oct 20 '15 at 11:06
-
@android please use `startActivityForResult()` as mentioned in previous comment. – Rohan Kandwal Oct 20 '15 at 11:09
-
-
Hi same another one problem i have one textview with green colour. After clicking one button i m changing textview colour to red. And i m minimizing that app and again opening from recents from mobile that textview colour showing as green colour. How....? – Sureshkumar S Oct 20 '15 at 11:11
-
@android Ask a different question for this problem. Please keep both problems separately. Your activity might be getting restarted, thus the state of TextView is changing. – Rohan Kandwal Oct 20 '15 at 11:13