3

I'm implementing a setting to my app that allows the user to switch themes, the user will be in the settings activity when he switches the theme, and above it in the task will be the main screen.

I want the current activity to restart after the theme change, but I also want the main screen to restart, so when the user hits back from the settings page, he'll see the main screen with the new activity.

Restarting the current activity is easy, but how do I force the previous activities in the current task to restart as well?

I've tried adding FLAG_ACTIVITY_CLEAR_TOP / FLAG_ACTIVITY_CLEAR_TASK to the intent, but it'll remove those activities from the task, so when I hit back from settings, I'm returned to the home screen and not to my app main screen.

Thanks.

Community
  • 1
  • 1
marmor
  • 27,641
  • 11
  • 107
  • 150
  • 1
    Would it be acceptable to restart the main activity when the user returns to it? Maybe, in `onActivityResult`? – Aleks G Sep 04 '12 at 12:15
  • That's a pretty good idea... I'll try it out. – marmor Sep 04 '12 at 12:19
  • As I think you know, system uses onConfigurationChange for that purpose. If system language will be changed - your activities will be either recreated, on onConfigurationChange() will be called, if you'll declare you can handle this case. I think in your case you should try to do the same, however, I'm not sure how to trigger COnfigurationChange due to your internal app setting. If it's possible to do so inside an app - it will be a perfect solution, I think. – AlexN Sep 04 '12 at 13:44
  • @AleksG I've went with your solution, if you'd like to post it as an answer I'll accept it. – marmor Sep 04 '12 at 15:14
  • @marmor Thanks. I posted the answer. – Aleks G Sep 04 '12 at 16:57

2 Answers2

2

Maybe you can check if you have any configuration changes onRestart method, so when you press back, this method should be fired.

ЯegDwight
  • 24,821
  • 10
  • 45
  • 52
van
  • 178
  • 5
  • This seems to be working, but I worry my isThemeChanged method is a bit heavy, so I wouldn't want to call it on every onRestart. Anyway, this is definitely a correct answer, but I'm looking into alternative ways before accepting this as the one. – marmor Sep 04 '12 at 13:58
2

One possible way would be to restart the main activity when the user returns to it. If you open your Preferences activity with startActivityForResult, then, when finishing that activity, you can use setResult to tell the main activity whether or not it needs to be restarted. Finally, in the main activity's onActivityResult you can look at the passed result and restart the main activity if necessary.

Aleks G
  • 56,435
  • 29
  • 168
  • 265