Use SharedPreferences to display popup only once

浪尽此生 提交于 2019-12-23 06:18:22

问题


I have the following code in my onActivityForResult method after I add a contact using an intent.

  if (mySharedPrefs.getBoolean("settingsPopup", false) == false) { //First time

            new AlertDialog.Builder(this)
                    .setTitle("Go to settings? ")
                    .setMessage("POPUP")
                    .setNegativeButton("No", null)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            Intent settingsIntent = new Intent(MainActivity.this, Settings.class);
                            startActivity(settingsIntent);

                        }
                    }).show();

            myEditor = mySharedPrefs.edit();
            myEditor.putBoolean("settingsPopup", true);
            myEditor.commit();

        }

I want this popup to only show once, which is why I set the shared preference key value "settingsPopup" to true after I first show the dialog. For some reason though, the dialog shows every time the onActivityForResult method gets called. Why does it show every time?

PS: I am using the same shared preference object for storing other values.

Edit

I initialize my shared prefs in onCreate like so:

mySharedPrefs = this.getSharedPreferences("sharedPrefsName", MainActivity.MODE_PRIVATE); //Making a shared preferences


回答1:


Create a class and call it SettingManager like following :

public class SettingsManager {
    public static final String DEFAULT_PREFERENCES_NAME = "defaultPreferences";

    public static final String PREFERENCE_FIRST_RUN = "isFirstRun";

    public static SharedPreferences getDefaultPreferences(Context context) {
        return context.getSharedPreferences(DEFAULT_PREFERENCES_NAME, Context.MODE_PRIVATE);
    }

    public static boolean isFirstRun(Context context) {
        SharedPreferences preferences = getDefaultPreferences(context);
        boolean isFirstRun = preferences.getBoolean(PREFERENCE_FIRST_RUN, true);
        preferences.edit().putBoolean(PREFERENCE_FIRST_RUN, false).commit();

        return isFirstRun;
    }

}

Then call it with something like this :

boolean isFirstRun = SettingManager.isFirstRun(getActivity());



回答2:


Try with putting code to store boolean variable before AlertDialog code:

if (mySharedPrefs.getBoolean("settingsPopup", false) == false) { //First time
            myEditor = mySharedPrefs.edit();
            myEditor.putBoolean("settingsPopup", true);
            myEditor.commit();

            new AlertDialog.Builder(this)
                    .setTitle("Go to settings? ")
                    .setMessage("POPUP")
                    .setNegativeButton("No", null)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            Intent settingsIntent = new Intent(MainActivity.this, Settings.class);
                            startActivity(settingsIntent);

                        }
                    }).show();


        }



回答3:


Try using apply() instead. Apply will update the preference object instantly and will save the new values asynchronously, so allowing you to read the latest values.

According to the documentation:

Unlike commit(), which writes its preferences out to persistent storage synchronously, apply() commits its changes to the in-memory SharedPreferences immediately but starts an asynchronous commit to disk and you won't be notified of any failures. If another editor on this SharedPreferences does a regular commit() while a apply() is still outstanding, the commit() will block until all async commits are completed as well as the commit itself.

It goes on to say:

As SharedPreferences instances are singletons within a process, it's safe to replace any instance of commit() with apply() if you were already ignoring the return value.



来源:https://stackoverflow.com/questions/35244256/use-sharedpreferences-to-display-popup-only-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!