How to have android app remember user data/profile? - android

Making a app that asks user to create a profile, wondering where I should get started in having the app remember this data user inputs? Any tutorials or suggestions would be appreciated.
Thanks,
Grant

I suggest you use SharedPreferences, unless you have a lot of information to store.
After the user successfully logged in, store his information in your Preferences.
For example, to store the username :
private SharedPreferences mPreferences;
mPreferences = getSharedPreferences("User", MODE_PRIVATE);
SharedPreferences.Editor editor = mPreferences.edit();
editor.putString("username", your_user_name);
editor.commit();
Each time the user accesses the login activity, you can check if the username is already stored in the preferences :
if (mPreferences.contains("username")) {
// start Main activity
} else {
// ask him to enter his credentials
}
When the user logs out, don't forget to delete the username key from your preferences :
SharedPreferences.Editor editor = mPreferences.edit();
editor.clear(); // This will delete all your preferences, check how to delete just one
editor.commit();

Insert the data to a SQLite database or use a plain file. The former is recommended for big apps.

Checked out SharedPreferences. Some examples here: SharedPreferences Tutorial
If you only have one user at once and just need to store simple user data like user name, email, id, you can store string/int/... format data in it. Or if you have server for storing user data, you can store credentials in SharedPreference and use the credentials to get data from your server.

Related

Closing the app and reopening it in the activity I want, without doing it all over again

I can not understand a thing.
I am creating an android app that is connected to the DB.
When the user registers after login, once he is logged in, if he closes the application and reopens it, he makes it all over again.
I want that when the user reopens the app if he has not done the Logout he must be in the activity of the Login.
Can you advise me what to use?
The SharedPreference?
Apps like Facebook, Instagram and other things use?
PS. They are many users.
Thank you very much for helping.
You can use SharedPrederence for session management of Login/Signup :
When User login Hit API or Internal DataBase with validation & get all user details & store it in SharedPreferences.
Next time when User again open app first it will check the status of sharedPreference if login get Data otherwise Login screen will come.
When User Logout Crear all Login Data.
Initialization :
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
Storing Data :
editor.putBoolean("login_status", true); // Storing boolean - true/false
editor.putString("name", "string value"); // Storing string
editor.putInt("user_id", "int value"); // Storing integer
editor.commit(); // commit changes
Retrieving Data :
pref.getString("name", null); // getting String
pref.getInt("user_id", 0); // getting Integer
Reference : https://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/

How to deal with multi accout login in android?

I have several apps which use the same accout.
The question is when i had more than one apps installed in my phone,how can i login without type username and password if one of the app had logged in.
See below reference code which is having app1 & app2, so using shared preferences suppose you can store username & password in app1 & you can using it in another app2. Here I'm shwing how to access username, you can expand it as per your requirement.
Okay! using this code in Application 1 ( with package name is "com.sharedpref1" ) to store data with Shared Preferences.
SharedPreferences prefs = getSharedPreferences("demopref",
Context.MODE_WORLD_READABLE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", strShareValue);
editor.commit();
And using this code in Application 2 to get data from Shared Preferences in Application 1. We can get it because we use MODE_WORLD_READABLE in application 1:
try {
con = createPackageContext("com.sharedpref1", 0);
SharedPreferences pref = con.getSharedPreferences(
"demopref", Context.MODE_PRIVATE);
String data = pref.getString("username", "No Value");
displaySharedValue.setText(data);
} catch (NameNotFoundException e) {
Log.e("Not data shared", e.toString());
}
I think, you could use Database for storing username and password, and share the database among your multiple apps, settings some field like is_logged_in with boolean value 1 when user is loggedin. On app launch, access the database, check for the field is_logged_in and display appropriate logged in view.

Retrieving SharedPreferences

I'm working with an android application, and when my application is open I want to log in. I'm using shared preferences for saving data.
How can I control three cases: if his credentials are null, if that user exists and does not have to register and save his credentials again and also if username and password he entered is correct and is the same with them he used when entered for the first time.
Use this code to retrieve values from sharedPreferences.
SharedPreferences sharedPref ;
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
int status=sharedPref.getInt("status",0);
if(status==1){
//do something here you want to do
}else if(status==2){
//do something here you want to do
}
Read the documentation here http://developer.android.com/reference/android/content/SharedPreferences.html
Also check this http://www.tutorialspoint.com/android/android_shared_preferences.htm

Does SharedPreferences work in Eclipse emulator?

I am attempting to save a user id using SharedPreferences. Do values saved as SharePreferences persist across all Activities in my application so I can access the userid from any application? Below is my code for saving the userid.
userid = result.substring(3, result.length());
Log.d("userid at onpostexecute", userid);
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit(); // to update userid
editor.putString("userid", userid);
editor.commit();
And here is the code for accessing the userid from the SharedPreferences in another activity.
SharedPreferences prefs = getPreferences(MODE_PRIVATE); // to access userid
String userid = prefs.getString("userid", "");
Log.d("shared prefs userid", userid);
What is strange is that the above code is in my onCreate method but it doesn't show up in the Logcat even though other log data is displayed before and after this code. So is there something wrong with my code that I can't even get it to display in my logcat? I can't even tell if it is being updated.
Values saved as sharedPreferences can be shared between activities if you tell it to. Right now you are creating a preference that is only accessible to that same activity. You need to use:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
Like this you are creating a sharedPreference between your application. There is a lot of explanation on the topic in the accepted answer to another question. Link to question
As discussed in the answer the way you are using to save the preference will only work in the same activity that saved it.
I assume you mean can you access them from any Activity, yes, they do persist even when you leave your app and come back. From the Docs
The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).
If this Log.d("userid at onpostexecute", userid); doesn't even show up then I would put a breakpoint there and make sure you have a value for userid. I would also check your logcat filters to make sure that you are getting all logs. Just make sure that the type in the spinner is set to "verbose" just to be sure

Android SharedPreferences

I have 3 webviews in my search engine app.When the user enters his query, by default he gets Google results, followed by yahoo and Ask buttons at the bottom, on clicking either of them, he gets results for that query from those sites. Now I want to give the user the privilege to change the default result site. I have created 3 radiobuttons. Upon confirmation, say he chooses Yahoo, how can i set it as Yahoo till the next time he changes it to some other site,
Accessing data from SharedPreferences:
SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
String webViewChoice = sharedPref.getString("userChoice","null");
if no choice was saved (in the case when the application is running for the first time), you'll get "null" in webViewChoice.
use this condition as you wish
Saving data in SharedPreferences:
SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString("userChoice",usersChoice);
prefEditor.commit();
I hope it helps.
Save the user's preference as default-engine=google by default in a shared preferences file.
On app loading, read the file and set the default engine during the app runtime. When user chooses a different engine as default, then update the preferences file.
Hope this helps.

Categories

Resources