Android: first popup launch dialog

I am trying to create a popup dialog that only appears after the first launch of the application, which will warn users about new changes in the application. So, I got a popup dialog box:

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

Once they reject it, it will not return until the next update or reinstall the application.

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

+24
android dialog
Sep 27 2018-11-11T00:
source share
5 answers

Use SharedPreferences to save the isFirstRun value, and check your launch activity against this value.

If the value is set, then there is no need to display a dialog. Otherwise, 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(); } } 
+69
Sep 27 '11 at 3:28
source share

While the general idea of ​​the other answers sounds, you should save in the general preferences not a logical, but a time stamp when there was the last time, when there was the first launch, or the latest version of the application for which this happened.

The reason for this is that you most likely want to launch the first launch dialog when the application was updated. If you save only the logical value, then when you update the application, the value will still be true, so the code will not be able to find out if it should run it again or not.

+19
Sep 27
source share

How can you change the difference between a new installation and an update.

 String StoredVersionname = ""; String VersionName; AlertDialog LoginDialog; AlertDialog UpdateDialog; AlertDialog FirstRunDialog; SharedPreferences prefs; public static String getVersionName(Context context, Class cls) { try { ComponentName comp = new ComponentName(context, cls); PackageInfo pinfo = context.getPackageManager().getPackageInfo( comp.getPackageName(), 0); return "Version: " + pinfo.versionName; } catch (android.content.pm.PackageManager.NameNotFoundException e) { return null; } } public void CheckFirstRun() { VersionName = MyActivity.getVersionName(this, MyActivity.class); prefs = PreferenceManager.getDefaultSharedPreferences(this); StoredVersionname = (prefs.getString("versionname", null)); if (StoredVersionname == null || StoredVersionname.length() == 0){ FirstRunDialog = new FirstRunDialog(this); FirstRunDialog.show(); }else if (StoredVersionname != VersionName) { UpdateDialog = new UpdateDialog(this); UpdateDialog.show(); } prefs.edit().putString("versionname", VersionName).commit(); } 
+7
Apr 15 '13 at 16:14
source share

I use this to display the help dialog box on first run. I do not want this to appear after the update. This is better suited for a change log.

 private void doFirstRun() { SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); if (settings.getBoolean("isFirstRun", true)) { showDialog(DIALOG_HELP); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("isFirstRun", false); editor.commit(); } } 
+3
Dec 09 2018-11-11T00:
source share

There is no urgent question, but I assume that you want to know how to achieve the intended effect. In this case, use the SharedPreference object to get the logical β€œfirst run”, which defaults to true, and set it to false immediately after the first run.

+2
Sep 27 2018-11-11T00:
source share



All Articles