我已经实现了Google文档(https://developer.android.com/guide/topics/ui/look-and-feel/darktheme)中的深色主题,并且一切正常。
但是!当应用程序关闭并收到新的通知时,当我打开它并使用NavDeepLinkBuilder以PendingIntent开头的应用程序启动时,即使选择了暗模式,该应用程序也始终以亮主题打开。
PendingIntent pendingIntent = new NavDeepLinkBuilder(getApplicationContext())
.setGraph(R.navigation.navigation_graph)
.setDestination(R.id.detailsFragment)
.setArguments(args)
.createPendingIntent();
notification.setContentIntent(pendingIntent);
public static void applyTheme(@NonNull String themePref)
{
switch(themePref)
{
case "light":
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
break;
case "dark":
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
break;
default:
if(BuildCompat.isAtLeastQ())
{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
else
{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
}
break;
}
}
}
public class MainActivity extends AppCompatActivity
{
private ActivityMainBinding mDataBinding;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
PreferencesManager.init(this);
applyTheme(); // this function calls the applyTheme above, ignore no parameter
mDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);