A simple question I just can’t find the answer…
I have view A and B, after clicking on a button on A, B shows up, and at this time if I click the back button on device, A will be shown again. Now I change something in B and hope it would affect A, e.g. setting A’s background color via a global variable, so that when the back button is pressed, A’s background color changes, how could I do this? What method is fired when A is shown from clicking the back button?
First you need to override the
onResume()method in A.onResume()will be fired once A is back to visible.Then you can implement
onBackPressed()method in your B, to save the settings you’ve made viaSharedPreferenceor something else. Then retrieve it on A’sonResume().Thus the whole solution will be like:
Acitivty A:
Activity B:
I think the Activity Lifecycle in Document could help you to figure out what is
onResume()and exactly when it will be fired.And
onBackPressed()in Documentation.Also SharedPreferences as well.