You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

自定义返回按钮导致静态布尔变量未持久化问题求助

Great question! Let's break down why this is happening and how to fix it:

Why the discrepancy happens

  • The system back button's default behavior calls finish() on your settings activity, which closes it and brings back the existing MainActivity instance from the task stack. Since the static isInDarkTheme variable lives in memory across your app's process, its modified value stays intact.
  • Chances are your custom back button is using an Intent to re-launch MainActivity (like startActivity(new Intent(this, MainActivity.class))). If MainActivity uses the default standard launch mode, Android creates a new instance of MainActivity. If you re-initialize isInDarkTheme in MainActivity's onCreate() (e.g., setting it to false by default), this new instance will overwrite your modified value. Even if you don't re-initialize, relying on static variables for state isn't a reliable "persistence" strategy to begin with.

Solutions (ordered by simplicity/reliability)

1. Make your custom back button match system behavior

The easiest fix is to replicate what the system back button does: just call finish() to close the settings activity and return to the existing MainActivity instance:

yourCustomBackBtn.setOnClickListener(v -> {
    finish();
});

This ensures the static variable's modified value stays preserved, exactly like the system back button.

2. Reuse the existing MainActivity instance (if you must use an Intent)

If you have a specific reason to launch MainActivity via Intent, configure it to reuse the already existing instance instead of creating a new one:

Option A: Add Intent flags
yourCustomBackBtn.setOnClickListener(v -> {
    Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
    // Clear all activities above MainActivity and reuse the existing top instance
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(intent);
    finish();
});
Option B: Set launch mode in AndroidManifest.xml

Update your MainActivity entry to use singleTop launch mode:

<activity
    android:name=".MainActivity"
    android:launchMode="singleTop">
    <!-- Other configuration -->
</activity>

This tells Android to reuse the existing MainActivity instance if it's already at the top of the task stack, instead of creating a new one.

3. Use true persistence with SharedPreferences

Static variables only hold their value while your app's process is alive—if the system kills your app to free memory, you'll lose the setting. For proper, long-lasting persistence, use SharedPreferences:

Step 1: Save the setting in your settings activity
// After updating isInDarkTheme
SharedPreferences themePrefs = getSharedPreferences("AppThemeSettings", MODE_PRIVATE);
themePrefs.edit()
        .putBoolean("isInDarkTheme", isInDarkTheme)
        .apply(); // Async save; use commit() for sync if needed

// Custom back button still uses finish()
yourCustomBackBtn.setOnClickListener(v -> {
    finish();
});
Step 2: Load the setting in MainActivity

Read the saved value in onResume() (or onCreate()) to ensure you always get the latest setting:

@Override
protected void onResume() {
    super.onResume();
    SharedPreferences themePrefs = getSharedPreferences("AppThemeSettings", MODE_PRIVATE);
    // Second argument is the default value if no setting exists yet
    isInDarkTheme = themePrefs.getBoolean("isInDarkTheme", false);
    // Update your UI/theme based on the loaded value
    applyAppTheme(isInDarkTheme);
}

This approach works reliably across app restarts, system kills, and any navigation flow.

内容的提问来源于stack exchange,提问作者MrMinemeet

火山引擎 最新活动