How can I redirect to another activity after returning from the screen lock or other app?

2

I have a simple app with a login, what I want is that in "x" number of activies when the user changes the application or the screen is blocked when returning to the app return to a specific activity (the login). For example: I am in the app in activity "A" and in the onStart () method or when I retrieve the visibility of the activity "A" but only if it comes from a screen lock or it recovers the first plane the app sends me to the activity of "B" (the login) and not "A".

Thanks in advance.

    
asked by Michael Emir 30.03.2016 в 00:41
source

1 answer

1

It occurs to me that this idea is the simplest, detecting when the screen has been blocked:

We add a variable to validate if the screen was blocked and another to validate when the application was changed:

 private boolean seBloqueoPantalla;
 private boolean cambioAplicacion;

within onPause() we change the value of the variable seBloqueoPantalla to "true", if we detect screen lock:

@Override
protected void onPause() {
    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    boolean isScreenOn = powerManager.isScreenOn();
    if (!isScreenOn) {
        Log.i(TAG, "La pantalla ha sido bloqueada");
        seBloqueoPantalla = true;
    }
    super.onPause();
}

and using the onStop() method we change the value of the variable cambioAplicacion to "true", if we detect that we change the application:

   @Override
    protected void onStop() {
        cambioAplicacion = true;
        super.onStop();
    }

Using the onResume() method, you can perform an "login" activity, depending on the state of the variable seBloqueoPantalla and cambioAplicacion :

@Override
protected void onResume() {
    if(seBloqueoPantalla && cambioAplicacion) {
        //Redirecciona a la actividad Login!.
        Intent myIntent = new Intent(this, LoginActivity.class);
        startActivity(myIntent);
        //reiniciamos valores.
        seBloqueoPantalla = false; 
        cambioAplicacion = false;
    }
    super.onResume();
}
    
answered by 30.03.2016 в 02:57