0

I want to show a special activity on 3rd launch of my app. I've made some researches and found this Check if application is on its first run. But I still don't know how to detect if it's a 3rd time or not and also in answer on that question was described how to know if app was stopped and then resumed but I need a solution that will show my special activity when user will open it on 3rd time!!!

Can somebody help me with this?

Thank you!

Community
  • 1
  • 1
S.Drumble4
  • 139
  • 8
  • Use SharedPreference, or Sqlite or Realm to store data and validate the same in Application starting Activity – Sreehari May 11 '16 at 10:09
  • Realm is not neccessary for that little data to save. SharedPreferences is exactly what You need. I suggest to store a integer value in onResume() every time activity comes to front. – Opiatefuchs May 11 '16 at 10:14

4 Answers4

1

In your launch activity put this code in onCreate method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Perhaps set content view here

    SaharedPreferences prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);

    int launch_count = prefs.getInt("launch_count", 0);

    if(launch_count>=3){
        // third time launch
        Intent intent = new Intent(this, NewActivity.class);
        startActivity(intent);
    } else {
        prefs.edit()
            .putInt("launch_count", launch_count+1)
            .apply();
    }
}

But this solution do not detect recreate activity, that will be reason to increment launch_count counter. You can solve this issue by creating "StartActivity", which increment counter and start main or specialy activity.

Gogi Bobina
  • 1,086
  • 1
  • 8
  • 25
0

In your Application class in onCreate method you can add code that reads integer value from Shared Preferences. If there is no value the value is 0. Then just add to it 1 and save. Then in any activity you can read this value. If value is 3 you good to go.

Adam Radomski
  • 2,515
  • 2
  • 17
  • 28
0

Better way to store the integer value in your sharedperference. Every time have to check whether the range has been come or not.

Example:

At first time, initialize int i=0 and store the value as 0

Next step, which means on the next time retrieve the value from shared preference and store in it. Then check the value.

if(i==range) //todo else //todo

0

keep the count in mysql db or shared preference ,and check and update on each launch of the app in splash screen or your first activity ,get the count and if(count>=3){ // do the operations what you need to here }else{ //first 3 opening of the app }

Jithu P.S
  • 1,843
  • 1
  • 19
  • 32