0

My application requires initial user setup, for the first time the app is opened by the user. He should be able to do some initial setup that will determine how the application is run. For this, I am using sharedPreferences. What Im doing is simply, setting the contentView depending on if the boolean is 'true' or false'. But the question i have is in regards to what happens after i setContentView().

//Variables
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    //creating sharedpref file
    PREFS_NAME = "MyPrefsFile"; 
    shPref = getSharedPreferences(PREFS_NAME, 0);
    shPref.edit().putBoolean("my_first_time", true).commit();
    //checking if app's being launched for first time
    if(shPref.getBoolean("my_first_time", true)) { 
        setContentView(R.layout.page_one);
        //setting the shPref as false
        shPref.edit().putBoolean("my_first_time", false).commit();
    }
    //checking if app has been launched before
    else if(shPref.getBoolean("my_first_time", false) && shprefchecker == 1) {
        setContentView(R.layout.home_page);
    }
}

So what I want to do is wait for the user to finish setup before I can set the shPref as false. Im guessing that once the layout is changed in this line " setContentView(R.layout.page_one);", it will automatically set the shPref as false. I want the app to not do anything until the user has gone through the initial setup (first layout in the intial setup is page_one.xml).

I might sound confusing, but how would I do this? Please help. All answers are appreciated. Thanks!

3 Answers3

0

I think it's pretty straight.

You can set the boolean to false on initial setup's Submit button click.

The set up page must have any button, right? So it will be ensured that user completes the setup first.

In your second activity, you will have to use:

shPref = getSharedPreferences(PREFS_NAME, MODE_WORLD_READABLE);
SharedPreferences.Editor pref_editor = shPref.edit();
pref_editor.putBoolean("first_time", false);
pref_editor.commit();
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • Yeah that makes sense. But how would I set the boolean to false if its part of a whole different class? –  Jun 27 '13 at 07:16
  • You can shared preferences in whole app. Its shared – MysticMagicϡ Jun 27 '13 at 07:17
  • But Im getting an error in my 2nd java file when I use this line of code in the onCreate() method: shPref.edit().putBoolean("my_first_time", false).commit(); it says "shPref cannot be resolved." I apologize for my lame mistakes, but this is the first time Im using SharedPreferences. –  Jun 27 '13 at 07:20
  • It makes more sense, but I'm still getting multiple errors regarding this. :/ –  Jun 27 '13 at 07:28
  • I put the code segment you gave me in the onCreate() of my second class which runs the user setup. "Multiple markers at this line - PREFS_NAME cannot be resolved to a variable - shPref cannot be resolved to a variable - The field Context.MODE_WORLD_READABLE is deprecated" –  Jun 27 '13 at 07:32
  • In your second activity you have to recreate another shared preferences, you won't be able to access to the one in the first activity. Android will make sure (if you put the same parameters, to recover the same shared_preferences for you. – zozelfelfo Jun 27 '13 at 07:33
  • Oh. Wow. makes sense. I'll try that and let you guys now. thanks ! –  Jun 27 '13 at 07:34
  • Okay, well I reinitialized the String PREFS_NAME and SharedPreferences shPref. All the errors are gone. is this correct? –  Jun 27 '13 at 07:36
0

The problem I see in your code is that your code is that you put my_first_time to true always. Just dont define it, if you dont define a var in your shared pref it will return the default value you pass as the second parameter. I'll edit your code:

Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    //creating sharedpref file
    PREFS_NAME = "MyPrefsFile"; 
    shPref = getSharedPreferences(PREFS_NAME, 0);
    //checking if app's being launched for first time
    if(shPref.getBoolean("my_first_time", true)) { //my first time not defined, it will be true
      setContentView(R.layout.page_one);
      //setting the shPref as false
      shPref.edit().putBoolean("my_first_time", false).commit(); //This line should go in your page_one activity when it ends, but when defined it will always go to home_page
    }
  //checking if app has been launched before
   else if(shPref.getBoolean("my_first_time", false) && shprefchecker == 1) {
      setContentView(R.layout.home_page);
   }
}

Hope it helps :)

zozelfelfo
  • 3,776
  • 2
  • 21
  • 35
  • Yes it does, thanks a lot! But Im getting the same problem I am discussing below in the other answer's comments. :/ –  Jun 27 '13 at 07:23
0

It is quite simple first You have to check bu getting shared preferences key value pair whether user has passed out through the setup process if not then You can start setup activity and if yes you can skip setup activity

Krishna
  • 51
  • 5