0

I'm trying to create a welcome screen. My problem is that everytime I re-open the application, it runs through the onCreate function again(code below) and re-initializes variables as if it were the first time again.I tried to use a counter with shared preferences but got the same result.My idea was that my application would run through the Oncreate method on the first load then create a boolean named firstBoot = true .Which would then be changed to false, but how can i test if a boolean is false if it does not exist yet? Any help would be greatly appreciated.

(within onCreate Method)

 final String PREFS_NAME = "MyPrefsFile";

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

if (settings.getBoolean("my_first_time", true)) {
    //the app is being launched for first time, do something
    Log.d("Comments", "First time");

    // first time task
    setContentView(R.layout.setup);

    // record the fact that the app has been started at least once
    settings.edit().putBoolean("my_first_time", false).commit();
}
else
{
    setContentView(R.layout.test);
}
sshashank124
  • 31,495
  • 9
  • 67
  • 76

1 Answers1

2

This code will help you to get what you need

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
    SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();

     int count = prefs.getInt("count", 0);
    count++;
            editor.putInt("count", count);
     editor.commit();

    if(count==1)
    { welcome screen code
    }
    else
    { your code
    }
vaibhav
  • 634
  • 1
  • 4
  • 23