1

I tried to make a dialog on startup of my application like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Hi Stackoverflow!").create().show();
}

But I want to make it so that if I click a checkbox in the dialog "Don't show again" then the second time I start the application the dialog doesn't appear. How can I do it?

a sandwhich
  • 4,352
  • 12
  • 41
  • 62
David_D
  • 1,404
  • 4
  • 31
  • 65

2 Answers2

2

If you just want a Dialog with a Button then you don't really need an AlertDialog. You can simply create a Dialog, create a layout for the Dialog in xml with the CheckBox and Button, then use setContentView() on the Dialog.

To not show the Dialog again simply create a boolean variable and use onCheckedChanged() to set that variable to true if the checkbox is checked. Save that in SharedPreferences and check that value on start up.

Good example of getting started with SharedPreferences

SharedPreferences complete docs

codeMagic
  • 44,549
  • 13
  • 77
  • 93
1

You can use SharedPreferences for this. Add a flag that tells you if the dialog has been shown before (or if the app has been launched before) and determine from there whether to show the dialog or not.

josephus
  • 8,284
  • 1
  • 37
  • 57