Skip to content Skip to sidebar Skip to footer

Reauthenticate On Restart - Android

I need reauthenticate user credentials every time onRestart is called (usually this means the user has locked&unlocked the screen or put it on background and then returned to i

Solution 1:

Well, I used this to solve it:

    private static final int REAUTHENTICATE = 80;

    private boolean authenticated;

    @Override
    public void onRestart() {
        if(authenticated)
            return;

        Intent intent = new Intent(this, LoginActivity.class);
        intent.setAction(LoginActivity.REAUTHENTICATE);
        startActivityForResult(intent, REAUTHENTICATE);
    }

    @Override
    public void onStop() {
        authenticated = false;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REAUTHENTICATE)
            authenticated = true;
    }

Well, this it no what I expected but works, I hope someone find a better solution. Cause onRestart keep getting called after onActivityResult.


Solution 2:

Until got a good solution/suggestion from any one can try this .

1- Put a boolean variable in shared pref named AuthentacationNeeded
2- get that in OnRestart with default value true 
3-if value is true then only startActivity
4- put that variable true in onpuase 

in Login activity 
4-   put that variable false Before finish()

Post a Comment for "Reauthenticate On Restart - Android"