Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: First run popup dialog

Tags:

android

dialog

I am trying to make a popup dialog that only shows after the app's first run that will alert users of the new changes in the app. So I have a dialog popup like this:

new AlertDialog.Builder(this).setTitle("First Run").setMessage("This only pops up once").setNeutralButton("OK", null).show(); 

Once they dismiss it, it won't come back until the next update or they reinstall the app.

How can I set the dialog code above to run only once?

like image 298
Nick Avatar asked Sep 27 '11 00:09

Nick


2 Answers

Use SharedPreferences to store the isFirstRun value, and check in your launching activity against that value.

If the value is set, then no need to display the dialog. If else, display the dialog and save the isFirstRun flag in SharedPreferences.

Example:

public void checkFirstRun() {     boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("isFirstRun", true);     if (isFirstRun){         // Place your dialog code here to display the dialog          getSharedPreferences("PREFERENCE", MODE_PRIVATE)           .edit()           .putBoolean("isFirstRun", false)           .apply();     } } 
like image 93
xDragonZ Avatar answered Sep 17 '22 15:09

xDragonZ


While the general idea of the other answers is sound, you should keep in the shared preferences not a boolean, but a timestamp of when was the last time the first run had happened, or the last app version for which it happened.

The reason for this is you'd likely want to have the first run dialog run also whenthe app was upgraded. If you keep only a boolean, on app upgrade, the value will still be true, so there's no way for your code code to know if it should run it again or not.

like image 36
Franci Penov Avatar answered Sep 21 '22 15:09

Franci Penov