Kakalokia
Kakalokia

Reputation: 3191

Saving information after closing the app

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.

Upvotes: 0

Views: 330

Answers (3)

Mohd Mufiz
Mohd Mufiz

Reputation: 2236

@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;
        }

Upvotes: 0

user370305
user370305

Reputation: 109237

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    
 }
}

Upvotes: 1

Mehul Ranpara
Mehul Ranpara

Reputation: 4255

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.

Upvotes: 0

Related Questions