如何在多个活动中实现夜间模式的保存?

问题描述

我尝试使用充气机检查“设置”活动中负责激活和停用夜间模式的开关是否已启用。重新启动应用程序后,MainActivity不会记住夜间模式状态。

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private Switch night_mode_sw;

    public static final String MyPREFERENCES = "nightModePrefs";
    public static final String KEY_ISNIGHTMODE = "isNightMMode";
    SharedPreferences sharedpreferences;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            final Toolbar toolbar = findViewById(R.id.toolbar);
            toolbar.getOverflowIcon().setColorFilter(16777215,PorterDuff.Mode.SRC_ATOP);
            setSupportActionBar(toolbar);

            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(R.layout.activity_settings,null);

            sharedpreferences = getSharedPreferences(MyPREFERENCES,Context.MODE_PRIVATE);

            night_mode_sw = findViewById(R.id.night_mode);

            checkNightModeActivated();

            view.findViewById(R.id.night_mode);

            night_mode_sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton,boolean isChecked) {
                    if (isChecked) {
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                        saveNightModeState(true);
                        recreate();
                    }
                    else {
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                        saveNightModeState(false);
                        recreate();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    private void saveNightModeState(boolean nightMode) {

            SharedPreferences.Editor editor = sharedpreferences.edit();

            editor.putBoolean(KEY_ISNIGHTMODE,nightMode);

            editor.apply();

    }

    public void checkNightModeActivated() {

            if (sharedpreferences.getBoolean(KEY_ISNIGHTMODE,false)) {
                night_mode_sw.setChecked(true);
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            }
            else{
                night_mode_sw.setChecked(false);
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.settings) {
            Intent myintent = new Intent(MainActivity.this,Settings.class);
            startActivity(myintent);
            return false;
        }

        if (id == R.id.aboutapp) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);

            builder.setTitle("About this app")
                    .setCancelable(false)
                    .setNeutralButton("GOT IT",new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface,int i) {
                            dialogInterface.cancel();
                        }
                    })

                    .setTitle("About this app")

                    .setMessage("awsd");

            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }

        return super.onOptionsItemSelected(item);

    }

}

Settings.java:

public class Settings extends AppCompatActivity {
    private Switch night_mode_sw;
    public static final String MyPREFERENCES = "nightModePrefs";
    public static final String KEY_ISNIGHTMODE = "isNightMMode";
    SharedPreferences sharedpreferences;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_settings);

            final Toolbar toolbar = findViewById(R.id.toolbar);
            toolbar.getOverflowIcon().setColorFilter(16777215,PorterDuff.Mode.SRC_ATOP);
            setSupportActionBar(toolbar);

            sharedpreferences = getSharedPreferences(MyPREFERENCES,Context.MODE_PRIVATE);

            night_mode_sw = findViewById(R.id.night_mode);

            checkNightModeActivated();

            night_mode_sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton,boolean isChecked) {
                    if (isChecked) {
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                        saveNightModeState(true);
                        recreate();
                    }
                    else {
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                        saveNightModeState(false);
                        recreate();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private void saveNightModeState(boolean nightMode) {

        SharedPreferences.Editor editor = sharedpreferences.edit();

        editor.putBoolean(KEY_ISNIGHTMODE,nightMode);

        editor.apply();
        }

        public void checkNightModeActivated() {
            if (sharedpreferences.getBoolean(KEY_ISNIGHTMODE,false)) {
                night_mode_sw.setChecked(true);
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            }
            else{
                night_mode_sw.setChecked(false);
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                }
            }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu1,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.calculator) {
            Intent myintent = new Intent (Settings.this,MainActivity.class);
            startActivity(myintent);
            return false;
        }

        if (id == R.id.aboutapp) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);

            builder.setTitle("About this app")
                    .setCancelable(false)
                    .setNeutralButton("GOT IT",int i) {
                            dialogInterface.cancel();
                        }
                    })

                    .setTitle("About this app")

                    .setMessage("awsd");

            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }

        return super.onOptionsItemSelected(item);
    }

}

我不确定这是什么原因。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...