lisovaccaro lisovaccaro - 1 year ago 78
Android Question

How to update activity after uses changes appearance from preferences?

When a user opens preferences on my app he may make changes for example changing the app theme.

The documentation for


Set the base theme for this context. Note that this should be called before any views are instantiated in the Context (for example before calling
inflate(int, ViewGroup)

So my first thought was restarting the app
when the user changed the preferences. However I noticed that sometimes the process of restarting the activity is seamless while other times the activity is closed, the home screen is seen and only after some seconds the app opens again.

I'm wondering if there is a way to change handle the preferences changes. Like for instance changing the theme after
without restarting the activity or restarting the activity on the background while the user is on preferences.

What's the right way to handle this?

Answer Source

I'm assuming you are storing user's selected theme in App Preferences. So in your Activity's onCreate(), add setTheme().

public void onCreate(Bundle savedInstanceState) {
    setTheme(; // or whatever theme you're using


But changes don't take effect until the activity is restarted. So if you need to immediately apply theme changes, call recreate() afterwards applying theme:

// Might need to call them on getApplication() depending on Context

Also, as Attiq mentioned, re-creating an activity might result in loss of data fetched from network using Threads/AsyncTasks so you need to take that in to account as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download