I am developing an Android App. It has a login page.. what is the best way to save users' details so they do not have to fill in their credentials every time they use the app.. according to Link ? we can use account manager or oath2. I am still not sure of which methods i can use.. I simply want the user to enter a username and a password once.. I came across other methods but I am looking for the best method to be used.
Asked
Active
Viewed 1,761 times
-1
-
3use sharedprefernces to achieve – Biraj Zalavadia Sep 12 '13 at 04:43
-
see my answer this will help you even i am also using this – Developer Sep 12 '13 at 04:49
-
@BirajZalavadia is correct and gaurav has given the answer you can use this – Sep 12 '13 at 04:54
-
But I think shared-preferences info will be removed if the device turns off. – Abed Elaziz Shehadeh Sep 12 '13 at 04:55
-
4@Azooz Totti SharedPreferences only removes when application Uninstalls – Biraj Zalavadia Sep 12 '13 at 04:57
-
@BirajZalavadia you are right – Developer Sep 12 '13 at 05:00
1 Answers
2
You can use this function to save username and password
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences shared = getSharedPreferences("shared", MODE_PRIVATE);
if(shared.contains("username") && shared.contains("password")){
startingActivity();
} else {
saveInformation(userId,pass);
}
}
public void saveInformation(String username,String password) {
SharedPreferences shared = getSharedPreferences("shared", MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.commit();
}

Developer
- 6,292
- 19
- 55
- 115
-
1Be cautious when saving passwords into sharedPreferences as they are stored within a plain text xml file; the above method works but will store it in plain text. Users with rooted devices can access this file and view the plain text password. See this page for a good overview of methods to protect data within your shared preferences: http://www.codeproject.com/Articles/549119/Encryption-Wrapper-for-Android-SharedPreferences – matthewrdev Sep 12 '13 at 05:29
-
@MattR thank you very much for this precious comment and thank you for helping me to learn more thanks alot – Developer Sep 12 '13 at 05:31
-
@MattR where i will find this shared preferences XML file in my device – Developer Sep 12 '13 at 06:30
-
Gaurav, see http://stackoverflow.com/questions/6146106/where-are-shared-preferences-stored – matthewrdev Sep 12 '13 at 08:17
-
-
@MattR i have seeen that post i want to see my shared pref data in the device i am not getting where i will get this – Developer Sep 12 '13 at 09:01
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37240/discussion-between-gaurav-and-matt-r) – Developer Sep 12 '13 at 09:04