1

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);
        }
    }
}
Nick J Adams
  • 125
  • 5
  • I believe this thread http://stackoverflow.com/questions/3007998/on-logout-clear-activity-history-stack-preventing-back-button-from-opening-l will be useful for you.. – Fadils Sep 29 '15 at 14:56

2 Answers2

0

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();
        } 
}
GingerHead
  • 8,130
  • 15
  • 59
  • 93
  • So basically you are saying that there is a fundemental issue with what I'm trying to do. I should move the check to a different stage of the workflow - when the actions on the screen are pressed for instance rather than the when the screen is displayed? – Nick J Adams Sep 29 '15 at 14:57
  • yes you are right, because like that you can never control the flow, if you want to exit, there is no way to tell home about your intentions, so it's better to put the login operation under a login button – GingerHead Sep 29 '15 at 15:06
0

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();
    }

}
JoeyJubb
  • 2,341
  • 1
  • 12
  • 19