Reputation: 637
How to display an AlertDialog
based on the number of execution of an activity(MainActivity
). For example if MainActivity
is opened for 5 times then i need to display an AlertDialog
.
Upvotes: 1
Views: 48
Reputation: 3692
Initialize your counter to 0 and increment it in onCreate()
and onResume()
methods of your activity. As soon as you increment these values, store these values in Shared Prefrences(as described in above answer). If you have trouble using Shared Preferences, try TinyDB, it is based on Shared Preferences and is much easier to handle.
Upvotes: 1
Reputation: 9223
Saving data in Preference:
private static void saveCounter(Context context, int value) {
SharedPreferences prefs = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("count", value);
editor.commit();
}
Retrieve data from preference:
private static int getCounter(Context context) {
SharedPreferences prefs = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
try {
return prefs.getInt("count", 0);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
These methods will make your work easy and you just have to pass the incremented value to saveCounter
for saving the value and then for getting the value use getCounter
Upvotes: 1
Reputation: 3873
Dear archana, Please save the varible/flag in SharedPreferences. Check varialble value (whether 5) then increment on every execution of activity until 5 and save to sharedprefrences and get it from there with every launch of activity. In oncreate method of activity please update the variable with increment+1 and save it and check it in next launch
For more visit:
http://www.tutorialspoint.com/android/android_shared_preferences.htm
Thanks
Upvotes: 1