1

I have two activities and one fragment in my app. The first activity, MainActivity, is used to hold the fragment. It contains an ActionBarSherlock ActionBar and the fragment, WFrag. WFrag opens up the second activity, SettingsActivity, when you press a button. In SettingsActivity, you can choose settings that affect the behavior of WFrag. Naturally, I want the onBackPressed() method to go back to WFrag and update it with the user's new settings. My question is: how do you make it so that when you go back (via the back button) from SettingsActivity, you recreate (call onCreate() again) WFrag?

Aneesh Ashutosh
  • 762
  • 7
  • 18

2 Answers2

1
  1. override onBackPressed or
  2. add the update in onStart/onResume
Gina
  • 902
  • 8
  • 15
1

First you have to override onBackPressed() of SettingActivity.

Second when you start second activity i.e SettingActivity from MainActivity you have to clear Stack of previous activity in this way

Intent intent = new Intent(this, SettingActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(intent);

Same goes for SettingActivity in onBackPressed() method you have to run your MainActivity in this way

Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(intent);

Or if you are redirecting to MainActivity from button click you can use above code for same.

Praveen
  • 388
  • 3
  • 7