簡體   English   中英

重新啟動應用程序時,SharedPreferences 不保存布爾值

[英]SharedPreferences not save boolean value when restart app

我正在開發我在 DarkMode 主題中使用的應用程序,我將Switch打開和關閉darkMode 並將其保存在 boolian whitch 中意味着darkMode 為真或假( boolean isDarkMode =false;

它運行良好,我使用SharedPreferences保存此 true 或 false 值以在其他活動中使用它。 它可以工作,但是......當我關閉應用程序並再次運行應用程序時,SharedPreferences 的值不會被保存,它會回到 boolian 的第一個值(謝謝你)

SettingActivity.java(設置 boolian true 或 false)

package com.kurdfoxx.nightmodewithsharedprefrense;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

public class SettingActivity extends AppCompatActivity {
    Switch mySwitch;
    boolean isDarkMode =false;
    private static final String DARKMODE = "login";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        myboolian();
        if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
            setTheme(R.style.darkTheme);
            isDarkMode =true;

        } else {
            setTheme(R.style.AppTheme);
            isDarkMode =false;


        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        Toast.makeText(this, ""+ isDarkMode, Toast.LENGTH_SHORT).show();
        mySwitch = findViewById(R.id.switch1);
        if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
            mySwitch.setChecked(true);
        }
        mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    isDarkMode =true;
                    restartApp();
                } else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    isDarkMode =false;

                    restartApp();

                }
            }
        });


        Button buttonGoMain=findViewById(R.id.btn_go_MainAct);
        buttonGoMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SettingActivity.this,MainActivity.class));

            }
        });


    }

    public void restartApp() {
        Intent intentRestartApp = new Intent(getApplicationContext(), SettingActivity.class);
        startActivity(intentRestartApp);
        finish();

    }
    public void myboolian(){
           isDarkMode =!isDarkMode;  //change isDarkMode from false to true by this
        SharedPreferences sharedPreferences =getSharedPreferences(DARKMODE,MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("ISDARKMODE", isDarkMode);
        editor.apply();


    }


}

和 MainActivity.java

package com.kurdfoxx.nightmodewithsharedprefrense;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private static final String DARKMODE = "login";
    boolean isDarkMode = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getData();
        if (isDarkMode){

            if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
                setTheme(R.style.darkTheme);
                isDarkMode =true;
            } else {
                setTheme(R.style.AppTheme);
                isDarkMode =false;

            }
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=findViewById(R.id.button_go_to_setting_activity);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,SettingActivity.class));
            }
        });

    }
    private void getData() {

        SharedPreferences sharedPreferences = getSharedPreferences(DARKMODE, MODE_PRIVATE);
        isDarkMode = sharedPreferences.getBoolean("ISDARKMODE", false);

    }

}

在您的 checkedChangedListener 中,您將 darkMode 分別設置為truefalse

並且在您的myboolian方法中是您寫入變量的唯一地方。

此方法僅從您的onCreate調用, onCreate能從其他任何地方調用。 除此之外,此方法始終設置darkMode = !darkMode ,交換值。

您必須在myboolian中調用myboolian並刪除darkMode = !darkMode否則您將始終重置為之前的值。

感謝@Grisgram 的幫助,我通過一些更改和解決了這個問題

設置Activity.java

package com.kurdfoxx.nightmodewithsharedprefrense;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

public class SettingActivity extends AppCompatActivity {
   public static Switch mySwitch;
    boolean isDarkMode =false;
    private static final String DARKMODE = "dark";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
            setTheme(R.style.darkTheme);
            isDarkMode=true;

        } else {
            setTheme(R.style.AppTheme);
            isDarkMode=false;


        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        Toast.makeText(this, ""+ isDarkMode, Toast.LENGTH_SHORT).show();
        mySwitch = findViewById(R.id.switch1);
        if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
            mySwitch.setChecked(true);
        }
        mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    isDarkMode=true;
                    myboolian();


                    restartApp();
                } else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    isDarkMode=false;
                    myboolian();

                    restartApp();

                }
            }
        });
        myboolian();

        Button buttonGoMain=findViewById(R.id.btn_go_MainAct);
        buttonGoMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SettingActivity.this,MainActivity.class));

            }
        });


    }

    public void restartApp() {
        Intent intentRestartApp = new Intent(getApplicationContext(), SettingActivity.class);
        startActivity(intentRestartApp);
        finish();

    }
    public void myboolian(){
     // isDarkMode =!isDarkMode;  //change isDarkMode from false to true by this
        SharedPreferences sharedPreferences =getSharedPreferences(DARKMODE,MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("ISDARKMODE", isDarkMode);
        editor.apply();


    }


}

和主要活動

package com.kurdfoxx.nightmodewithsharedprefrense;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final String DARKMODE = "dark";
    boolean isDarkMode ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getData();
        Toast.makeText(this, "is datk ?"+isDarkMode, Toast.LENGTH_SHORT).show();

        if (isDarkMode){
                setTheme(R.style.darkTheme);
        }else {setTheme(R.style.AppTheme);}
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=findViewById(R.id.button_go_to_setting_activity);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,SettingActivity.class));
            }
        });

    }
    private void getData() {

        SharedPreferences sharedPreferences = getSharedPreferences(DARKMODE, MODE_PRIVATE);
        isDarkMode = sharedPreferences.getBoolean("ISDARKMODE", false);



    }




}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM