Reputation:
I made a Shared Preferences system that will check if the user launches the app for the first time, if so, it will launch a AlertDialog
public boolean onCreateOptionsMenu(Menu menu){
// Make MenuInflater
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
inflater.inflate(R.menu.main, menu);
SharedPreferences settings = getSharedPreferences("prefs", 0);
boolean firstRun = settings.getBoolean("firstRun", true);
if ( firstRun )
{
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setTitle("Nieuwe gebruiker?");
ad.setMessage("- Als u naar de volgende dag wilt kun u de knop morgen gebruik in het menu.\n - In het menu kunt u ook terug naar vandaag en u kunt de pagina refreshen. \n - U kunt zelfs de roosterwijzigingen delen via WhatsApp of via E-Mail.");
ad.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
ad.show();
}
// Return True
return true;
}
But I didnt make it good I think, because it launches the AlertDialog everytime you start the app. What did I wrong and how do I solve it?
Upvotes: 0
Views: 2269
Reputation: 558
I did something similar and I solved it by creating a sqlite database in which when the app is first time opened I check if the database table is empty if it is I do something and insert data to database if there is data in database I do nothing .
Upvotes: 1
Reputation: 2348
Change your code to
....
boolean firstRun = settings.getBoolean("firstRun", true);
if ( firstRun ){
settings.edit().putBoolean("firstRun", false).commit(); //set your flag to false
....
Upvotes: 2
Reputation: 82543
You aren't changing the value of the firstRun
in the SharedPreferences to false after you've shown the dialog.
In your AlertDialog's OK button, save firstRun
in SharedPreferences to have the value of false
, so that the if statement is not triggered again.
Upvotes: 1