Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to recreate the activity?

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?

like image 871
Enrichman Avatar asked Dec 29 '25 03:12

Enrichman


1 Answers

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);
like image 103
Kuffs Avatar answered Dec 30 '25 18:12

Kuffs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!