Frage

In my app, i am starting a service in Mainactivity. I show a reminder popup everytime the app is opened. But, I only want to show the pop up 1 time.

How to store the pop up's count accros multiple app launches?

War es hilfreich?

Lösung

boolean mboolean = false;

SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
mboolean = settings.getBoolean("FIRST_RUN", false);
if (!mboolean) {
 // do the thing for the first time 
  SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putBoolean("FIRST_RUN", true);
                    editor.commit();                    
} else {
 // other time your app loads
}

This code will show something only once, untill you dont reinstall app or clear app data

EXPLANATION:

OK, I'll explain it to you. SharedPreferences are like a private space of your application in which you can store primitive data (strings,int,boolean...) that will be saved untill you dont delete application. My code above works like this, you have one boolean which is false when you start the application, and you will show your popup ONLY if the boolean is false --> if (!mboolean). Once you showed your pop up, you put the boolean value to true in sharedPreferences, and system will next time check there, see that it is true and wont show the pop up again untill you reinstall the application or clear the application data from application manager.

Andere Tipps

put this code when you push popup.

pref = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());

    if (!pref.getBoolean("popupfirst", false)) {
        Editor editPref = pref.edit();
        editPref.putBoolean("popupfirst", true);
        editPref.commit();
    }

when your app first start and you push popup then its add true in to Preference else it can't do anything.

Store it in the SharedPreferences.

E.g. to save it in your shared preferences, when showing the popup:

// Get the shared preferences
SharedPreferences prefs = getSharedPreferences("com.example.app", Context.MODE_PRIVATE);

// Edit the shared preferences
Editor editor = prefs.edit();

// Save '1' in a key named: 'alert_count'
editor.putInt("alert_count", 1);

// Commit the changes
editor.commit();

And afterwards, when you launch the app, you can extract it again, to check how many times it was launched before:

// Get the shared preferences
SharedPreferences prefs = getSharedPreferences("com.example.app", Context.MODE_PRIVATE);


// Get the value of the integer stored with key: 'alert_count'
int timesLaunched = prefs.getInt("alert_count", 0); // 0 is the default value

if(timesLaunched <= 0){
    // Pop up has not been launched before
} else {
    // Pop up has been launched 'timesLaunched' times
}

Ideally, when you save the number of times launched in the SharedPreferences, you can first extract it, and than increment the current value.. Like so:

SharedPreferences prefs = getSharedPreferences("com.example.app", Context.MODE_PRIVATE);

// Get the current value of how many times you launched the popup
int timesLaunched = prefs.getInt("alert_count", 0); // 0 is the default value
Editor editor = prefs.edit();
editor.putInt("alert_count", timesLaunched++);
editor.commit();
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top