androidsharedpreferencesapplication-restart

android, why save to prefs does not work if restart app too soon?


In android app, having an option menu with a checkbox item, and when clicking on the checkbox to toggle it would like to re start the app for picking up some change to start with.

So when the checkbox is clicked, in the onOptionsItemSelected it saves the check state to the prefs, then re start the app. But seems when app restarts and the reading from the prefs it still has the check state unchanged (same as what it was before the click).

However if delay the restart for 100 ms, then in the restart it will get the saved check state properly.

Anything wrong with how the app is restarted?



    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
           
            case R.id.is_checked:

                boolean checked = !item.isChecked();

                Log.w("+++", "+++ onOptionsItemSelected(), !item.isChecked() - checked: "+checked);

                item.setChecked(checked);
                updateIsCheckedInRegistry(checked);  //<=== save to prefs

                boolean isChecked_reg = getIsCheckedFromRegistry(); //<=== read from the registry to verify
                Log.w("+++", "+++  after save, make sure reading from the prefs, reg - isChecked_reg: "+isChecked_reg);

                //restartApp(this);  //<=== immediately call does not work, after restart app still having the old check state

                final Context THIS = this;
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        restartApp(THIS);
                    }
                };
                new Handler().postDelayed(runnable, 100);  // delay 50 ms does not work either

                
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    public static void restartApp(Context context) {
        PackageManager packageManager = context.getPackageManager();
        Intent intent = packageManager.getLaunchIntentForPackage(context.getPackageName());
        ComponentName componentName = intent.getComponent();
        Intent mainIntent = Intent.makeRestartActivityTask(componentName);
        context.startActivity(mainIntent);
        
        Runtime.getRuntime().exit(0);
    }
        
    private boolean getIsCheckedFromRegistry() {
        return PreferenceManager.getDefaultSharedPreferences(this)
                .getBoolean(PREFS_DEMO_IS_CHECKED, true);
    }
    
    private void updateIsCheckedInRegistry(boolean isChecked) {
        Log.w("+++", "+++ updateIsCheckedInRegistry(), isChecked: "+isChecked);

        PreferenceManager.getDefaultSharedPreferences(this).edit()
                .putBoolean(PREFS_DEMO_IS_CHECKED, isChecked).apply();
    }

Solution

  • Because you're using apply. Apply does it in the background to avoid blocking the thread. If you want to write it immediately, use commit() instead.