简体   繁体   中英

show custom dialog only once with onclick Button

i'm trying to show my custom dialog only once ; firstly i have a custom dialog contains one button OK when i click ok button and re open app i want to hide the custom dialog for that i tried to use SharedPreference

   SharedPreferences.Editor editor;
    SharedPreferences pref;
    @SuppressLint("CommitPrefEdits")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pref = getApplicationContext().getSharedPreferences("MyPref", 0);
        editor = pref.edit();
        editor.putBoolean("key_name", false);

/**/

 if (pref.getBoolean("key_name", true)) {
                users();
  }

private void users() {
        final Dialog myDialog = new Dialog(MainActivity.this);
        myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        assert myDialog.getWindow() != null;
        myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        myDialog.setCancelable(false);
        myDialog.setContentView(R.layout.users_artwork_dialog);
        Button okbtn = myDialog.findViewById(R.id.okbtn);

        okbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
                @SuppressLint("CommitPrefEdits") SharedPreferences.Editor editor = pref.edit();
                editor.putBoolean("key_name", false);
                myDialog.dismiss();
            }
        });
        myDialog.show();
    }

You should do editor.commit() or editor.apply() to save the changes to SharedPreference in users() method. That should solve the problem.

You should either commit() or apply() SharedPreference editor.

void commit() :
Commit your preferences changes back from this Editor to the SharedPreferences object it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.

void apply () Commit your preferences changes back from this Editor to the SharedPreferences object it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.

Check your modified code as well

    SharedPreferences.Editor editor;
    SharedPreferences pref;

    @SuppressLint("CommitPrefEdits")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pref = getApplicationContext().getSharedPreferences("MyPref", 0);
        editor = pref.edit();
        editor.putBoolean("key_name", true);
        //Add commit()
        editor.commit();

/**/

        if (pref.getBoolean("key_name", false)) {
            users();
        }
    }
    private void users() {
        final Dialog myDialog = new Dialog(MainActivity.this);
        myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        assert myDialog.getWindow() != null;
        myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        myDialog.setCancelable(false);
        myDialog.setContentView(R.layout.users_artwork_dialog);
        Button okbtn = myDialog.findViewById(R.id.okbtn);

        okbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
                @SuppressLint("CommitPrefEdits") SharedPreferences.Editor editor = pref.edit();
                editor.putBoolean("key_name", false);
                //Add commit()
                editor.commit();
                myDialog.dismiss();
            }
        });
        myDialog.show();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM