1

The theme changes in current activity(Settings Activity), but I have to restart the other background activities to apply the new theme. I'm thinking that the background activities should be restarted in the settings activity, but I can't find how.

This is my Settings Activity:

public class SettingsActivity extends FragmentActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

    private SharedPreferences prefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SettingsHelper.onActivityCreateSetTheme(this);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(android.R.id.content, new SettingsFragment())
                .commit();

        prefs = PreferenceManager.getDefaultSharedPreferences(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        prefs.registerOnSharedPreferenceChangeListener(this);

    }

    @Override
    protected void onPause() {
        super.onPause();
        prefs.unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals("theme")) {
            boolean isDark = prefs.getBoolean("theme", false);
            prefs.edit().putBoolean("theme", isDark).apply();
            SettingsHelper.changeToTheme(this);
        }
    }
2eety
  • 89
  • 1
  • 13

4 Answers4

2
switch (cTheme)
{
    case BLACK:         
        int myTheme = R.style.Default
        activity.setTheme(myTheme);

        //Save your activity theme color
        saveTheme(myTheme);
    break;

    case YELLOW:
        int myTheme = R.style.Green
        activity.setTheme(myTheme);

        //Save your activity theme color
        saveTheme(myTheme);
    break;
}
and change your onActivityCreateSetTheme(Activity activity) to:

public static void onActivityCreateSetTheme(Activity activity, Int cTheme)
Save method

public void saveTheme(int theme)
{
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    prefEditor.putInt("Theme",theme); 
}
Load method

public int loadTheme(){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    //Load theme color
    int theme = sharedPreferences.getInt("Theme",Color.RED); //RED is default color, when nothing is saved yet

    return theme;
}
Important: call loadTheme() before setContentView() so your onCreate() should be like:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  int theme = loadTheme();        //Load your theme here!!!!
  CustomazationProcess.onActivityCreateSetTheme(this, theme);
  setContentView(R.layout.something1);

  findViewById(R.id.black).setOnClickListener(this);
  findViewById(R.id.yellow).setOnClickListener(this);
}
Dishant Chanchad
  • 710
  • 3
  • 9
  • 27
  • 2
    Hello Dishant, code only answers are not recommended. I suggest that you add more details and explanation so we can understand you answer very well – Atef Hares Mar 21 '19 at 06:55
  • I have a helper class that does this, but I have to call on my all activities either in (onStart or onResume) a recreate method, but this enters in an infinite loop, hence the question. – 2eety Mar 21 '19 at 07:10
1

The answer was indeed to recreate the activity but only after checking that the value got onCreate is different from the one onResume. Link to a related SO question: Refresh(recreate) the activities in back stack when change locale at run time

@Override
    protected void onCreate() {
        super.onCreate()
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        isDark = preferences.getBoolean("theme", false);
    }

@Override
    protected void onResume() {
        super.onResume();
        if(isDark != preferences.getBoolean("theme", false))
            recreate();
    }
2eety
  • 89
  • 1
  • 13
0

In all of your activities, try to put below code in onResume() method:

@Override
public void onResume(){
    super.onResume();
    boolean isDark = prefs.getBoolean("theme", false);
    if(!isDark){
        recreate();
    }    
}

recreate() needs to be invoked to recreate the activity so that the new theme can be applied on the current screen.

android
  • 2,942
  • 1
  • 15
  • 20
0

There is nothing called restart activity while it is in background and still keep it in background.

you can simply finish this activity so next start will be using the new theme.

or if your theme can be manually applied then you can apply the new theme at it's onResume (or onStart, depending on your logic).

You can also recreate the activity at onResume (if theme changed) but I don't recommend this as this resume will be unnecessary and you can simply use first option.

Atef Hares
  • 4,715
  • 3
  • 29
  • 61
  • From what I've read I can apply a theme on another activity only onCreate(), calling setTheme in either onStart() or onResume() doesn't change my theme. – 2eety Mar 21 '19 at 07:02
  • No, You have misunderstood me. I am not saying to use `setTheme` at `onResume` or `onStart` I meant that if you app changes it's colors only for example and not re-setting the whole activity theme then this can be done manually at these methods. but since your app seems to require re-setting it's activity theme then you can go with one of the other two options – Atef Hares Mar 21 '19 at 07:06