I am writing an application with login details (username and password).
I want to make it so that when the user closes the application and starts it again later he/she d
OK so far I think your code is correct but you dont know where to implement it.
So there should be a point where your login is successful and you want to open another Activity in your app and for that you must be calling an Intent.
Therefore all you need to do is when you fire the Intent on successful login you can call your saveLoginDetails() method like this.
if(*successful login condition*){
Intent intent=new Intent(CurrentActivity.this,NextActivity.class);
saveLoginDetails();
startActivity(intent);
}
and whenever the user logout you can implement your removeLoginDetails() method.
Hope it helps... Cheers... :)
I suggest, you save the login details in a file with simple encryption.
password = 1234qwer encryptedPassword = 149250351452q113w119e101r114
OnStop SaveLoginData
OnStart ReadLoginData
Try this way: defined Preferences first
private static final String PREFS_NAME = "preferences";
private static final String PREF_UNAME = "Username";
private static final String PREF_PASSWORD = "Password";
private final String DefaultUnameValue = "";
private String UnameValue;
private final String DefaultPasswordValue = "";
private String PasswordValue;
And onPause()
@Override
public void onPause() {
super.onPause();
savePreferences();
}
And onResume()
@Override
public void onResume() {
super.onResume();
loadPreferences();
}
And here savePreferences()
private void savePreferences() {
SharedPreferences settings = getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
// Edit and commit
UnameValue = edt_username.getText();
PasswordValue = edt_password.getText();
System.out.println("onPause save name: " + UnameValue);
System.out.println("onPause save password: " + PasswordValue);
editor.putString(PREF_UNAME, UnameValue);
editor.putString(PREF_PASSWORD, PasswordValue);
editor.commit();
}
And here loadPreferences()
private void loadPreferences() {
SharedPreferences settings = getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
// Get value
UnameValue = settings.getString(PREF_UNAME, DefaultUnameValue);
PasswordValue = settings.getString(PREF_PASSWORD, DefaultPasswordValue);
edt_username.setText(UnameValue);
edt_password.setText(PasswordValue);
System.out.println("onResume load name: " + UnameValue);
System.out.println("onResume load password: " + PasswordValue);
}
I high not recommend your logic ! I recommend you toAccountManager API to authenticate and store the users credentials, or write your own account manager, always use this AccountManager which only stores your auth-token.
Account Manager's data can also be accessed through a root!
For your logic at least try to to use SharedPreferences in secured way otherwise with root access we can get SharedPreferences data from the mobile!