0

my app has a really weird issue: In a PreferenceActivity I provide under a (Sub)PreferenceScreen a ListPreference where the user selects "light" or "dark". So I set up a OnPreferenceChangedListener and when the user triggers it, I will recreate the Prefs.. That works. But pressing the back button will show the underlying Activity in the old theme configuration. So how to manage this?

Martin Pfeffer
  • 12,471
  • 9
  • 59
  • 68

1 Answers1

0

Found a solution.. Now I'm finishing MainActivity and in the Prefs I overrode the BackButton. This seems to work quite well. Maybe anybody has a other solution. I'm not quite happy with the overridden BackButton - in earlier days I got a little trouble by that "trick".

 themePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                changetheme();
                return true;
            }
        });
    } 


    private void changetheme() {
        recreate();
    }


   @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        Intent backToMain = new Intent(SettingsActivity.this, MainActivity.class);
        backToMain.putExtra("data", "test");
        startActivity(backToMain);
    }
Martin Pfeffer
  • 12,471
  • 9
  • 59
  • 68