当您单击复选框时,应用程序主题更改不起作用,并且不会重新启动。 我尝试使用方法doRestart()重新启动并重新创建。但是它最初几次都起作用。那么什么都不起作用
这是我的主要活动
public abstract class Navigation extends AppCompatActivity {
private static String TAG;
private DrawerLayout drawer;
private ActionBarDrawerToggle t;
private NavigationView nv;
private static Context context;
private int theme = R.style.AppTheme;
private static String THEME_TAG = "Navigation.THEME";
@LayoutRes
protected int getLayoutResId() {
return R.layout.activity_fragment;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int theme_tmp = savedInstanceState== null? theme : savedInstanceState.getInt(THEME_TAG, theme);
setTheme(theme_tmp);
setContentView(getLayoutResId());
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
Boolean themeB = pref.getBoolean("theme", true);
if (themeB){
theme = R.style.AppTheme;
} else {
theme = R.style.Light;
}
context = getApplicationContext();
pref.registerOnSharedPreferenceChangeListener((sharedPreferences, key) -> {
if ("theme".equals(key)){
setTheme(theme);
recreate();
//doRestart(context);
}
});
drawer = findViewById(R.id.drawer_layout);
t = new ActionBarDrawerToggle(this, drawer, R.string.Open, R.string.Close);
drawer.addDrawerListener(t);
t.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
nv = findViewById(R.id.nav_view);
nv.setNavigationItemSelectedListener(item -> {
int id = item.getItemId();
switch (id) {
case R.id.account:
openFragment("account");
drawer.closeDrawers();
break;
/*case R.id.recipes:
openFragment("R.id.recipes");
break;*/
case R.id.settings:
openFragment("settings");
drawer.closeDrawers();
break;
default:
return true;
}
return false;
});
}
public void openFragment(String frag) {
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentByTag(frag);
if (fragment == null) {
switch (frag) {
case "account":
fragment = new AccountFragment();
break;
/* case "R.id.recipes":
break;*/
case "settings":
fragment = new SettingsFragment();
break;
}
fm.beginTransaction()
.replace(R.id.container_fragments, fragment, frag)
.commit();
}
}
public void doRestart(Context c) {
try {
//check if the context is given
if (c != null) {
//fetch the packagemanager so we can get the default launch activity
// (you can replace this intent with any other activity if you want
PackageManager pm = c.getPackageManager();
//check if we got the PackageManager
if (pm != null) {
//create the intent with the default start activity for your application
Intent mStartActivity = pm.getLaunchIntentForPackage(
c.getPackageName()
);
if (mStartActivity != null) {
mStartActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//create a pending intent so the application is restarted after System.exit(0) was called.
// We use an AlarmManager to call this intent in 100ms
int mPendingIntentId = 223344;
PendingIntent mPendingIntent = PendingIntent
.getActivity(c, mPendingIntentId, mStartActivity,
PendingIntent.FLAG_CANCEL_CURRENT|PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager mgr = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
startActivity(getIntent());
finish();
} else {
System.out.println("Was not able to restart application, mStartActivity null");
}
} else {
System.out.println("Was not able to restart application, PM null");
}
} else {
System.out.println("Was not able to restart application, Context null");
}
} catch (Exception ex) {
System.out.println("Was not able to restart application");
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putSerializable(THEME_TAG, theme);
super.onSaveInstanceState(outState);
}
}
setttings.xml
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/personalization">
<CheckBoxPreference
android:key="@string/theme"
android:title="@string/checkbox_dark_theme">
</CheckBoxPreference>
</PreferenceCategory>
<ListPreference
android:entries="@array/sizes"
android:entryValues="@array/sizes_values"
android:key="list_sizes"
android:summary="@string/font_size_summary"
android:title="@string/font_size">
</ListPreference>
和此settengfFragment
公共类SettingsFragment扩展了PreferenceFragmentCompat {
private int theme = R.style.AppTheme;
@Override
public void onCreatePreferences(Bundle bundle, String s) {
addPreferencesFromResource(R.xml.settings);
}
}