-4

How can I show an AltertDialog after 3 launches once? I only know how to do something at the first start of the App(I am a beginner with Android and Sharedpreferences):

if (settings.getBoolean("my_first_time", true)) {
     Log.d("Comments", "First time");

     // Action which is done at first launch
     // save first launch
     settings.edit().putBoolean("my_first_time", false).commit();
} else {
     //not first time          
}

Thank's, Felix

glennsl
  • 28,186
  • 12
  • 57
  • 75
Seilbahn
  • 13
  • 3

2 Answers2

1

You can do it by updating sharedPreferences by 1 and check if it is equal to 3, if it is then show alertDialog.

Here is an rough example.

In your onCreate method add this.

int appLaunchedFor = getSharedPreferences("app_info", MODE_PRIVATE).getInt("counter", 0);
if(appLaunchedFor == 3){
    //Launch AlertDialog;

    //Now increament by 1 so that alertDialog will not launch again.
    getSharedPreferences("app_info", MODE_PRIVATE).edit().putInt("counter", ++appLaunchedFor).commit();
}else if (appLaunchedFor < 3){
    getSharedPreferences("app_info", MODE_PRIVATE).edit().putInt("counter", ++appLaunchedFor).commit();
}
Nishant Bhakta
  • 2,897
  • 3
  • 21
  • 24
0

The simplest way to do this is by using SharedPreferences, since it's simple and you said in the comments you've already used it.

You can do that by subclassing the Application class, incrementing a counter inside MyApplication#onCreate() and saving it in the SharedPreferences.

Note: This is not 100% certain. If the user clears the app's data, your counter will be lost. To prevent that, you can use private databases such as SQLite and Realm.

Gabriel Costa
  • 341
  • 1
  • 10
  • "if the user clears the app's data", every kind of data will be lost except for those which were saved to [external storage](https://developer.android.com/guide/topics/data/data-storage.html#filesExternal). As "private databases" can't be saved to external storage (which is not private), they too would be erased. – Bö macht Blau Oct 16 '17 at 17:26
  • @0X0nosugar well, that's not entirely correct. Both SQLite and Realm **can** be saved in external storage ([SQLite source][1], [Realm source][2]). In that case, he'd need the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions. [1]: https://stackoverflow.com/questions/14373863/how-to-store-sqlite-database-directly-on-sdcard [2]: https://stackoverflow.com/questions/29551664/how-to-move-my-realm-file-in-sd-card – Gabriel Costa Oct 16 '17 at 17:37
  • Thanks for ur answer but I dont know how i should save it in sharedpreferences. I am a beginner in Android – Seilbahn Oct 16 '17 at 17:42
  • Well, I don't like the idea of storing sensitive data in external storage (not even encrypted sensitive data) but I suppose a counter is not exactly sensitive. And one could store a countdown and clean up after the third time - to keep the user's device tidy ;-) So okay, it's possible. – Bö macht Blau Oct 16 '17 at 17:43
  • @Seilbahn SharedPreferences use a key-value approach. You can set a key (say "COUNTER") to your counter and when your Application class' onCreate() is called, you can retrieve that counter (using the specified key), increment it, checking if == 3 and save it, if necessary. – Gabriel Costa Oct 16 '17 at 17:48
  • 1
    @0X0nosugar I agree. Even though it is possible, creating a database that will remain there even after the app is uninstalled is bad. But, in this case, it seems to be some sort of coursework. In that case, SharedPreferences should be fine. – Gabriel Costa Oct 16 '17 at 17:50