I was able to open the app and it starts by opening the login screen and after login it takes you to the MainActivity. From MainActivity there is no back to login and it would stay in session until logout. But when I exit the app it would go back to the login screen even tho I have a Sharedpref which saves the username in session. I added nohistory=true in the manifest. In MainActivity I put Intent mainactivity.class(if press back button) this reason was because when I:
logout > login > mainactivity
And press back button it would go back to logout screen (login should be okay because of myhistory=true). My question is how can I keep username in session even if the app is exit out.
Links below doesn’t help me much
What is the correct way of creating a login screen/activity in Android?
Prevent showing Login screen after user login
How to presist Login credentials and do auto-login in Android
My code
LOGIN.java
public class Login extends Activity {
private EditText etUsername;
private Button btnLogin;
private Button btnCancel;
private TextView lblResult;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_BACK)
{
this.startActivity(new Intent(Login.this,Login.class));
}
return true;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
btnLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String username = etUsername.getText().toString();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", username);
if(username.equals("1111")){
lblResult.setText("Login successful.");
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
Logout.java
public class LogoutActivity extends Activity {
private Button btnLogout;
private Button btnCancel;
private TextView lblResult;
private EditText code;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_BACK)
{
this.startActivity(new Intent(LogoutActivity.this,MainActivity.class));
}
return true;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logout);
code = (EditText)findViewById(R.id.codeout);
btnLogout = (Button) findViewById(R.id.submit);
btnCancel = (Button) findViewById(R.id.cancel);
lblResult = (TextView)findViewById(R.id.result);
btnLogout.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String logout = code.getText().toString();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.remove("username");
if (logout.equals("99999")){
lblResult.setText("Logout successful");
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);
} else {
lblResult.setText("Logout failed");
}
}
});
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
});
In
Login.onCreate(), check for the user ID in the SharedPref. If it exists, go directly to theMainActivity.