3

I've implemented two simple themes on my app, you can select them inside a SettingsActivity (extending PreferenceActivity).

At this point, when you change theme, it's applied only on the new created activities because the activity from where you called the settings is an old one in the activity stack.

I've searched a lot and I've found this pretty useful: how to restart an activity.

By the way, I'm not completely clear on WHERE put this code. The only way to make it works was to put it in the onRestart() method,

but this is a HUGE wasting of cpu, battery and user experience.

Any help?

Community
  • 1
  • 1
Enrichman
  • 11,157
  • 11
  • 67
  • 101
  • what exactly is your question? – Kuffs Jul 30 '12 at 13:32
  • I need to apply the new selected theme to all the activities. To achieve this I need to recreate also the old ones. How can I do this? – Enrichman Jul 30 '12 at 13:35
  • Force the user back to the start of the app if the theme changes so all activities can be recreated. – Kuffs Jul 30 '12 at 13:36
  • Ok, how can I do this? And where? Basically now the user can enter in the settings, change the theme and go back with the back button (the setting are called from a OptionsMenu). – Enrichman Jul 30 '12 at 13:39

2 Answers2

1

I would register a listener for onPreferenceChanged. In there you can check to see if your theme preference was the one that was changed. Then you can just restart your Activity by using the following code.

Intent intent = getIntent();
finish();
startActivity(intent);

By grabbing the intent with getIntent() you make sure that your Activity will start the exact same way.

Frank Sposaro
  • 8,511
  • 4
  • 43
  • 64
1

Set a PreferenceChangedListener on the theme preference. If it gets fired, clear your user back to the top level of the app. You could just restart the current activity but if you are several activities deep then this becomes a bit of a pain. Easier to just reset the app back to the start.

        Intent i = new Intent(this, MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        this.finish();
        this.startActivity(i);
Kuffs
  • 35,581
  • 10
  • 79
  • 92