简体   繁体   中英

Can't use back button to leave Android app due to redirect

I'm struggling to allow the users to leave my app using the back button after logout.

My HomeActivity checks that the user has a sessionID. if they don't it will redirect them to the login page. upon successful login the homepage is displayed showing the sessionID and a logout button.

Clicking on the button removes the session ID and displays a message to the user to that effect. Pressing the back button at this point takes the user to the login page, and I can't get off it by pressing back - I can login and press back to leave the app, but I can't quit using the back button after logout.

Any Ideas?

HomeActivity

public class HomeActivity extends Activity {
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
}

protected void onStart(){
    super.onStart();
    /*
     * Check if a session ID exists - if it does then display the home screen 
     * If the session ID does not exist then redirect the user to the login page.
     */
    String sessionId = Session.getSessionId(getApplicationContext());
    if (StringUtils.isBlank(sessionId)){
        navigateToLoginActivity();
    }

    // Display the Home Screen
    setContentView(R.layout.home);

    TextView tv = (TextView)findViewById(R.id.welcome);

    tv.setText(sessionId);
}

public void logout(View view){
    navigateToLogoutActivity();
}

public void navigateToLoginActivity(){
    Intent loginIntent = new Intent(getApplicationContext(),LoginActivity.class);
    loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(loginIntent);
}

public void navigateToLogoutActivity(){
    Intent logoutIntent = new Intent(getApplicationContext(),LogoutActivity.class);
    logoutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(logoutIntent);
}
}

LoginActivity

public class LoginActivity extends Activity {
EditText nfcET;

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    nfcET = (EditText)findViewById(R.id.nfckey);
}

public void login(View view){
    String nfckey = nfcET.getText().toString();
    RequestParams params = new RequestParams();
    params.put("nfckey", nfckey);
    invokeWS(params);
}

public void invokeWS(RequestParams params){
   // REMOVED WEBSERVICE CODE - calls webservice and retrieves a sessionID which is saved in a SharedPreference, it calls navigateToHomeActivity upon successful login


}

public void navigatetoHomeActivity(){
    Intent homeIntent = new Intent(getApplicationContext(),HomeActivity.class);
    homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(homeIntent);
}
}

LogoutActivity

public class LogoutActivity extends Activity{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Session.removeSessionId(getApplicationContext());

        if (!StringUtils.isBlank(Session.getSessionId(getApplicationContext()))){
            //TODO - Handle the case that the session was not removed!
        } else {
            setContentView(R.layout.logout);
        }
    }
}

This is the part where you are having difficulties:

if (StringUtils.isBlank(sessionId)){
        navigateToLoginActivity();
}

Whenever you need to go out of the app by clicking the back button, it's the home page which will always be visited last. And because homepage will always check if you are logged in or not, and redirect you to the login page in case of the latter, you are really stuck and real time ;)

override the key down event:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch(keyCode){
    case KeyEvent.KEYCODE_BACK:
        // do something here 
        // Here you can put your boolean variable
          isOutOfApp = true;
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

and here can do:

if (StringUtils.isBlank(sessionId)){
        if(isOutOfApp) {
           // show a confirmation message 
           // and out of the app
        } else {
           navigateToLoginActivity();
        } 
}

To get out of the loop, ask the LoginActivity to log you in and return whether or not it was successful using startActivtyForResult like this:

In HomeActivity:

Change the navigateToLoginActivity to look like this:

public void navigateToLoginActivity(){
    Intent loginIntent = new Intent(getApplicationContext(),LoginActivity.class);
    startActivityForResult(loginIntent, REQ_LOG_IN);
}

In LoginActivity:

Add this to your onCreate:

setResult(RESULT_CANCELED);

Alter your navigatetoHomeActivity to this:

public void navigatetoHomeActivity(){
    setResult(RESULT_OK);
    finish();
}

Back in HomeActviity:

Move this block to onResume() (because onActivityResult is called before onResume)

    /*
     * Check if a session ID exists - if it does then display the home screen 
     * If the session ID does not exist then redirect the user to the login page.
     */
    String sessionId = Session.getSessionId(getApplicationContext());
    if (StringUtils.isBlank(sessionId)){
        navigateToLoginActivity();
    }

Add this method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(requestCode == REQ_LOGIN && resultCode == RESULT_CANCELED){
        finish();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM