27

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?

Nick
  • 9,285
  • 33
  • 104
  • 147
  • possible duplicate of [Determine if android app is the first time used](http://stackoverflow.com/questions/4636141/determine-if-android-app-is-the-first-time-used) – Jannie Theunissen Jun 11 '15 at 07:32

6 Answers6

74

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();
    }
}
xDragonZ
  • 12,502
  • 6
  • 37
  • 52
  • 2
    for this to work, I needed to change `getSharedPreference` to `getSharedPreferences` – gMale Sep 05 '13 at 06:37
  • What should be the entry in the manifest to get this to work? – Sndn Feb 08 '15 at 09:06
  • 1
    You don't need to add anything to your manifest get this to work. Just make sure your activity is declared in your manifest is sufficient. – xDragonZ Feb 08 '15 at 10:09
19

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.

Franci Penov
  • 74,861
  • 18
  • 132
  • 169
7

Like this you can make a difference between a new install and a 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();
}
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Mark
  • 156
  • 1
  • 7
3

I use this to display a help dialog on first run. I don't want this to show after an update. That is better suited for a changelog.

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();
        }
}
IT-Dan
  • 565
  • 5
  • 15
2
 loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
 loginPrefsEditor = loginPreferences.edit();
 doFirstRun();

 private void doFirstRun() {
        SharedPreferences settings = getSharedPreferences("PREFERENCE", MODE_PRIVATE);
        if (settings.getBoolean("isFirstRun", true)) {
            loginPrefsEditor.clear();
            loginPrefsEditor.commit();
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("isFirstRun", false);
            editor.commit();
        }
    }

I used this code to ensure that its the first time someone runs the application. The loginPrefsEditor is cleared from data because i have a "Remember Me" Button which stores the data to the sd card. Hope it helps !

Athanas
  • 21
  • 3
2

There is no actual question, but I assume you want to know how to achieve the intended effect. If that's the case, then use a SharedPreference object to get a boolean 'first run' which defaults to true, and set it to false just after the first run.

K-ballo
  • 80,396
  • 20
  • 159
  • 169