Android 启用/禁用夜间模式时,如何从其他活动重新创建活动?
我发现了许多与此相关的问题,但我的问题有点不同 在SettingActivity中,我通过以下代码启用/禁用nigh模式:Android 启用/禁用夜间模式时,如何从其他活动重新创建活动?,android,android-activity,android-theme,Android,Android Activity,Android Theme,我发现了许多与此相关的问题,但我的问题有点不同 在SettingActivity中,我通过以下代码启用/禁用nigh模式: ApplicationClass.setNightMode(); ApplicationClass.setDayMode(); 在应用程序类中编写的实际代码 但是当我从SettingActivity返回MainActivity时,由于onCreate()方法不调用,布局的颜色不会改变 启用/禁用夜间模式时,如何从SettingActivity重新创建MainActivit
ApplicationClass.setNightMode();
ApplicationClass.setDayMode();
在应用程序类中编写的实际代码
但是当我从SettingActivity返回MainActivity时,由于onCreate()方法不调用,布局的颜色不会改变
启用/禁用夜间模式时,如何从SettingActivity重新创建MainActivity
启用和禁用夜间模式的应用程序代码:
public static void setNightMode(){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
public static void setDayMode(){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
public class MainAcitivy extends ... {
.....
@Override
public void onResume(){
super.onResume();
if(nightModeOn) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
}
如果重新创建MainActivity,则会丢失其当前状态,因此可以尝试覆盖onResume并检查是否需要启用/禁用夜间模式:
public static void setNightMode(){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
public static void setDayMode(){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
public class MainAcitivy extends ... {
.....
@Override
public void onResume(){
super.onResume();
if(nightModeOn) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
}
另一种解决方案是通过覆盖SettingActivity和start MainActivity中的onBackPressed来启动新的MainActivity:
public class SettingsActivity extends .. {
.....
@Override
public void onBackPressed() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finishAffinity();
}
}
您的
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE\u NIGHT\u YES)代码>应位于应用程序级别。而且,在你的应用程序之间进行任何活动时,回头或往前看都不会有问题。别忘了把它登记到舱单上
例如:
public class TestApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//give a start value AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
public static void setNightMode(){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
public static void setDayModeOrSth(){
//AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
}
在您的清单文件中:
<application
android:name="package.name.TestApplication"
或
说出我愚蠢的名字和例子:)但事情会是这样的。祝你好运 您可以覆盖onConfigurationChanged活动方法
public abstract void onconfiguration changed(Configuration newConfig)
您可以在onResume中设置调用以禁用MainActivity中的夜间模式,检查android AcitCity生命周期:@medyas您的意思是什么?请解释一下。你能分享一下或举个例子吗?我试过你的代码,但没用。我用代码更新了问题。您可以查看它。在您的onResume
中放置以下内容:AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.getDefaultNightMode())
在MainActivity onResume()中添加了以下代码,但仍然无法工作AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.getDefaultNightMode())代码>比我不知道的多。您不需要重新创建活动。只要实现onConfigurationChanged方法,只要配置发生更改,控制就会自动转移到那里。您能分享一下这个例子吗?