0

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?

boreas
  • 1,041
  • 1
  • 15
  • 30

2 Answers2

0

You should make explicit return from B. So there should be a color chooser and OK button. The button should close activity B and return value for A. Then A will retrieve the value in onActivityResult method.

The back button is not intended to provide result. Therefore if you want the behavior you described. Then when someone chooses color in B activity, save it immediately in Shared Preferences (remember to name the file). Then read in the same preference file in onResume method in activity A and set the background accordingly.

Aleksander Gralak
  • 1,509
  • 10
  • 26
0

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 via SharedPreference or something else. Then retrieve it on A's onResume().

Thus the whole solution will be like:

Acitivty A:

@Override
public void onResume() {
super.onResume();
//blah blah
//Retrieve SharedPreferences or the saved data from B
}

Activity B:

@Override
public void onBackPressed() {
//save your data or settings here
}

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.

dumbfingers
  • 7,001
  • 5
  • 54
  • 80