Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save login Information locally in android

Tags:

android

I have made an android app which working fine. I implemented the Login functionality in it. Can anyone guide me how to store the login credentials of the user so that the user does not have to login everytime he/she starts the app and when user logout then clear the stored login information.

Actually i want that user does not have to enter login information everytime when he/she starts the app.How can i do this?

like image 267
nikki Avatar asked Mar 19 '12 13:03

nikki


People also ask

Where can we store sensitive data in Android?

In general sensitive data stored locally on the device should always be at least encrypted, and any keys used for encryption methods should be securely stored within the Android Keystore. These files should also be stored within the application sandbox.


2 Answers

Store value use sharepreference. i am pasting code here..use it.

Create Share Preference:

SharedPreferences sp=getSharedPreferences("Login", MODE_PRIVATE);
SharedPreferences.Editor Ed=sp.edit();
Ed.putString("Unm",Value );              
Ed.putString("Psw",Value);   
Ed.commit();

Get Value from Share preference:

SharedPreferences sp1=this.getSharedPreferences("Login", MODE_PRIVATE);

String unm=sp1.getString("Unm", null);       
String pass = sp1.getString("Psw", null);

When user logout then you can set null value into sharedpreference.

like image 149
Hasmukh Avatar answered Nov 10 '22 22:11

Hasmukh


I think the best solution is Shared Preference. It is useless to create a whole Database and query it, for just one entry. And File System doesnt provide a structured solution. (key value pairs)

For UI, I think that you should have two Fragments if the user is registered show him The MainFragment and if not the LoginFragment

like image 1
Michael Marinos Likouras Avatar answered Nov 11 '22 00:11

Michael Marinos Likouras