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


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

1 answer


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:

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;

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

    protected void onStop() {
        cambioAplicacion = true;

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

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