Search code examples
androidandroid-studioback

Back button issue in login module


Let say I have 2 forms in my app. let say loginForm and mainForm. In loginForm allows the user to enter his/her username and password then goes to mainForm. From mainForm is I click the back button it will go back to loginForm and now my issue is from loginForm if I will click back button it goes again to mainForm without logging in.

What is the best to make this correct?

loginForm

EditText edtU = (EditText) findViewById (R.id.txtU);
EditText edtP = (EditText) findViewById (R.id.txtP);
Button btnLogin = (Button) findViewById (R.id.btLog);
btnLogin.setOnclickListener(new OnclickListener).......{
    if(u.matches("ryan") && p.matches("biugos"){
      Intent i = new Intent(getApplicationContext(),mainForm.class);
      startActivity(i);
    }
}

Solution

  • After passing the intent use finish like

    startActivity(new Intent(context, ActivityName.class));
    finish();
    

    and use on back pressed to close the system.

      @Override
    public void onBackPressed() {
        super.onBackPressed();
        System.exit(1);
    }