Throwing an android prompt window only first time you run the app comes up again and again for many different situations so I thought I make a blogpost about it.

Android prompt window only first time you run the app

In the following example I needed to throw up a prompt for the user the very first time they run the app to ask them if they would like to see a tutorial about how to use the app.

I have two activities, MainActivity and TutorialActivity. The MainActivity is throwing the prompt to ask you if you would like to go to the tutorial activity.

It checks for a key keyTutorial in the Android Shared Preference. I use Prefs variable to access the shared preference globaly within the MainActivity class. Be sure to initialize Prefs with your package name! instead of YOUR.OWN.APPLICATION.PACKAGENAME.COM

Step 1: I check if I need to ask the user about the tutorial in method promptTutorial() that returns a Boolean, which will be implemented later. If so I throw the dialog window and send the user off their way to the next activity if they answered YES. Some exceptions are handled for theming the dialog using Build.VERSION.SDK_INT.

// Declare Prefs as global in MainActivity so you can access it from it's other methods
private SharedPreferences Prefs;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

/// continue

    Prefs = this.getSharedPreferences("YOUR.OWN.APPLICATION.PACKAGENAME.COM", Context.MODE_PRIVATE);

    if (promptTutorial()) {
        // Tutorial was never prompted
        AlertDialog.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
        } else {
            builder = new AlertDialog.Builder(this);
        }
        // Build prompt dialog
        builder.setTitle("Welcome to the USC Residential Experience App")
                .setMessage("Would you like to view a brief tutorial on how to use the app?")
                .setPositiveButton(R.string.tutorial_yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // do something if user clicked YES
                        Intent intent = new Intent(MainActivity.this, TutorialActivity.class);
                        startActivity(intent);
                    }
                })
                .setNegativeButton(R.string.tutorial_no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .show();
    }

}

Step 2: Implement promptTutorial() which is called to decide if the dialog window needs to be prompted.

Basically what it does is: it checks for keyTutorial ins Shared prefs.

If it finds it there it will return false, because the dialog was already prompted at some point.

If it does not find it there it will return it with the value of NullTutorial, so basically it’s not there and returns true, but before returning true it will save keyTutorial with the value or PROMPTED so next time it knows it already prompted the dialog. For this it uses a function called saveKey(key, value) which we will implement in the next step.

// The function that decides if you need to prompt the dialog window
    private boolean promptTutorial() {
        // Check fo saved value in Shared preference for key: keyTutorial return "NullTutorial" if nothing found
        keyTutorial = Prefs.getString("keyTutorial", "NullTutorial");
        // Log what we found in shared preference
        Log.d(TAG, "Shared Pref read: [keyTutorial: " + keyTutorial + "]");

        if (keyTutorial.contains("NullTutorial")){
            // if nothing found save a new value "PROMPTED" for the key: keyTutorial
            // to save it in shared prefs just call our saveKey function
            saveKey("keyTutorial", "PROMPTED");
            return true;
        }
       // if some value was found for this key we already propted this window some time in the past
      // no need to prompt it again
        return false;
    }

Step 3: Implement Save Key, to save values in the Android Shared Preference.

// The SaveKEy function
private void saveKey(String key, String value) {
        SharedPreferences.Editor editor = Prefs.edit();
        // Log what are we saving in the shared Prefs
        Log.d(TAG, "Shared Prefs Write ["+ key + ":" +value + "]");
        editor.putString(key, value);
        editor.commit();
    }

 

That’s pretty much it. Like the blog post using the ♥ icon if you found it useful! Cheers!