Search code examples
androidauthenticationonresumeonpause

Android Login Screen after Application enters foreground again. Login Screen is NOT root Activity


I'm programming a Voting Android Application. After the user has chosen an Election where he wants to make a vote, the user gets redirected to a Login Activity where he has to enter a password. After that, a Ballot Activity gets started and the person can choose some candidates. A requirement in this project is, that if the application leaves the foreground (onPause()) and enters the foreground back later (onResume()), the login screen should return and ask the user for his credentials again. How does one realize that? I saw some solutions at stackoverflow with android:clearTaskOnlaunch="true", but this does only work if the login screen is the root activity, which in my case is not true. Can someone help me out? Thank you.

P.S: I'm sorry for my bad english, I'm not a native speaker.


Solution

  • You just need to call finish() everytime you launch LoginActivity or BallotActivity.

    private void go2Login(){
       Intent i = new Intent (BallotActivity.this, LoginActivity.class);
       startActivity(i);
       finish();
    }
    
    void onPause(){
       go2Login();
    }
    

    BUT I think this is not a good design. User will be angry if he has to enter password everytime the app pauses. I think you should store his password in Preferences instead.