0

I'm using SettingsActivity as preference activity for my application like so:

public class SettingsActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(getString(R.string.dark_theme_key), false)) {
            setTheme(R.style.AppThemeDark);
        } else {
            setTheme(R.style.AppThemeLight);
        }

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .replace(android.R.id.content, new SettingsFragment())
                    .commit();
        }
    }

    public static class SettingsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.app_preferences);

            ...
        }
    }
}

If I use getActivity().recreate(); (I have a SwitchPreference for dark and light theme. Dark theme is Theme.AppCompat and light is same but with .Light. If I change the theme I use recreate.) or change orientation of the screen (Resulting in onCreate of course), I get a weird problem when I'm on my light theme.

The problem is as follows -after a recreate, the text content of the EditTextPreference's turn white instead of black.

If I simply ignore savedInstanceState == null and just create a new fragment every time on SettingsActivity.onCreate, I lose the "screen state" (for example if I was scrolled all the way down before, after recreate Im scrolled back to the top. If I had a EditTextPreference open before, after recreate its closed).

I'm not sure how to restore a fragment screen state in case of recreation, or why my problem even occurs. Would appreciate help.

Ungoliant
  • 133
  • 2
  • 14
  • Just out of interest, does the issue manifest if you set the theme before calling `super.onCreate(savedInstanceState);`? – clownba0t Nov 14 '16 at 05:34
  • Thanks for the reply. I shall try it when I get home. – Ungoliant Nov 14 '16 at 12:13
  • Awesome! it actually worked. Thanks a lot! – Ungoliant Nov 14 '16 at 14:24
  • You're of course welcome to post your answer and Ill flag it as one and upvote :) – Ungoliant Nov 14 '16 at 14:30
  • Ah, actually, I'm afraid my comment really was just a question/thought experiment rather than a suggested answer. I had a feeling it might work and I'm glad that it seems to, but I also have a nagging feeling that it may not be addressing the root cause of the issue. Also, there are potential pitfalls to touching the activity before running `super.onCreate` (see [this post](http://stackoverflow.com/a/9626268/2259854)), so please do be careful if you decide to continue with this approach. It may not work as expected in all usage scenarios across all devices and Android versions. – clownba0t Nov 14 '16 at 15:38
  • Anyway, I'm sorry I can't give you a more solid answer. I've just about reached the limits of my knowledge, but I'll keep thinking about it - maybe something will spring to mind! In the meantime, perhaps someone else may have a better idea :) – clownba0t Nov 14 '16 at 15:39

0 Answers0