0

I've been trying to create a user profile section for my app where the user enters his information (Name, DoB, height, weight etc) and clicks submit. After this he's taken to the main menu of the app.

The problem I have is that if I close the app and run it again, this will obviously result in the app displaying the user profile section again and asks the user to enter his information.

I've been trying to look for a way in which the app saves the information that the user enters and remembers it. So for example when the user first uses the app, he gets the user profile section and enters his information. When the user closes the app and opens it again it should take him straight away to the main menu.

I know I could achieve this slightly with Preferences, but I'd rather use a normal layout(LinearLayout) so that it gives me more options such as TextView etc.

Is there a way where I could achieve this using just LinearLayout instead of Preferences?

I've also been looking at creating custom Preferences, but none of the things I found was particularly useful.

Thanks in advance.

Kakalokia
  • 3,191
  • 3
  • 24
  • 42
  • So you just want to display it on screen, and don't want to save it in database? – MysticMagicϡ Nov 19 '12 at 13:02
  • Yeah I want to display to the user a section where he enters the information required. The information he enters should then be saved(somehow) so that I can use the information to make decisions later on such as taking the user to the main menu straight away when he opens the app for the second time. – Kakalokia Nov 19 '12 at 13:05

3 Answers3

1

Use SharedPreferences.

  1. Check the application for First Run and display layout which you want to enter user's profile.

  2. After store boolean flag for first Run in shared Preference which will prevent your Profile Screen to display again.

Look at Check if application is on its first run

Update:

Put this FirstRun() code in your onCreate() of Main Activity.

private void FirstRun()
{
 SharedPreferences settings = this.getSharedPreferences(MainActivity.PREFS_NAME, 0);
 boolean firstrun = settings.getBoolean("firstrun", true);
 if (firstrun)
 {
  // Checks to see if we've ran the application b4

  SharedPreferences.Editor e = settings.edit();
  e.putBoolean("firstrun", false);
  e.commit();
  // Display User Profile Screen    
 }
}
Community
  • 1
  • 1
user370305
  • 108,599
  • 23
  • 164
  • 151
  • What if I want to store the information the user enters so that I can make decisions later on based on them. – Kakalokia Nov 19 '12 at 13:09
  • Store those information in `SharedPreference`. Android provides `SharedPreference` for the same reason what you need. – user370305 Nov 19 '12 at 13:10
  • OK Thanks. I'll just need to understand how to use SharedPreference now. Is there a tutorial that shows a complete example of using SharedPreference with TextView and EditText? – Kakalokia Nov 19 '12 at 13:21
  • Look at http://developer.android.com/guide/topics/data/data-storage.html#pref and http://android-er.blogspot.in/2011/01/example-of-using-sharedpreferencesedito.html. – user370305 Nov 19 '12 at 14:17
0

use sharedPreference to store information by this way..

        final SharedPreferences pref1 = getSharedPreferences("myapp", MODE_PRIVATE);
        SharedPreferences.Editor editor = pref1.edit();
        editor.putString("userid", "success");
        editor.commit();

and to get the value from it use below code..

final SharedPreferences pref1 = getSharedPreferences("myapp", MODE_PRIVATE);
String str1= pref2.getString("userid", null);

or try to use SqliteDatabase or may be Application Class..this will store the information as you want.

Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39
0
@Override
public boolean saveUserData(UserModel userModel, Context context) {


    email = userModel.getEmail();
    firstName = userModel.getFirstName();
    lastName = userModel.getLastName();
    twitterId = userModel.getTwitterId();

    SharedPreferences userData = context.getSharedPreferences(APP_NAME,
            Context.MODE_PRIVATE);
    SharedPreferences.Editor setUserDataPreference = userData.edit();

    setUserDataPreference.putString(EMAIL, email);
    setUserDataPreference.putString(FIRST_NAME, firstName);
    setUserDataPreference.putString(LAST_NAME, lastName);
    setUserDataPreference.putString(TWITTER_ID, twitterId);

    setUserDataPreference.commit();

    return true;
}




 @Override
        public UserModel getUserData(Context context) {
            UserModel userModel = new UserModel();

            SharedPreferences userData = context.getSharedPreferences(APP_NAME,
                    Context.MODE_PRIVATE);

            email = userData.getString(EMAIL, "");
            firstName = userData.getString(FIRST_NAME, "");
            lastName = userData.getString(LAST_NAME, "");
            twitterId = userData.getString(TWITTER_ID, "");


            userModel.setEmail(email);
            userModel.setFirstName(firstName);
            userModel.setLastName(lastName);
            userModel.setTwitterId(twitterId);

            return userModel;
        }
Mohd Mufiz
  • 2,236
  • 17
  • 28