Razvi
Razvi

Reputation: 398

How to imitate finishAffinity in Android?

The question is pretty self explanatory.

I'm doing a class that registers the activity of the user in order to avoid inactivity, after 2 minutes of no activity or everytime the user minimizes the app, the app should close. My code is the one listed below:

public class InActivityTimer extends Activity {

    public static final long DISCONNECT_TIMEOUT = 2*60*1000; // 2 min = 2 * 60 * 1000 ms
    public static int ESTADO_ACTIVIDAD=0; //Variable para saber si la actividad está al frente o no.

    private static Handler disconnectHandler = new Handler(){
        public void handleMessage(Message msg) {
        }
    };

    private Runnable disconnectCallback = new Runnable() {
        @Override
        public void run() {
            // Perform any required operation on disconnect
            //finish();
            finishAffinity();
            Intent intent = new Intent(getApplicationContext(),VentanaInactividadCierreAplicacion.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

            //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }
    };

    public void resetDisconnectTimer(){
        disconnectHandler.removeCallbacks(disconnectCallback);
        disconnectHandler.postDelayed(disconnectCallback, DISCONNECT_TIMEOUT);
    }

    public void stopDisconnectTimer(){
        disconnectHandler.removeCallbacks(disconnectCallback);
    }

    @Override
    public void onUserInteraction(){
        resetDisconnectTimer();
    }

    @Override
    public void onResume() {
        super.onResume();
        ESTADO_ACTIVIDAD ++;
        resetDisconnectTimer();
        comprobarEstado();
    }

    private void comprobarEstado() {

        if (ESTADO_ACTIVIDAD == 0){
//            Intent startMain = new Intent(getApplicationContext(), SplashScreen.class);
//            startMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
//            startActivity(startMain);
//            finish();
            finishAffinity();
            Toast.makeText(getApplicationContext(), "RandomPassGEN cerrado por seguridad", Toast.LENGTH_SHORT).show();}
    }

    @Override
    public void onStop() {
        super.onStop();
        ESTADO_ACTIVIDAD--;
        stopDisconnectTimer();
        comprobarEstado();
    }

 /*   @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(getBaseContext(),"La aplicación se reiniciará por seguridad", Toast.LENGTH_SHORT).show();
        finishAffinity();
    }*/

}

As you can see, I'm using finishaffinty twice. How could I do it withous using that command? I'd like to avoid it because using finishAffinity() needs android +4.1

Thank you so much for your help.

Upvotes: 3

Views: 3375

Answers (1)

David Wasser
David Wasser

Reputation: 95636

To close your application, do the following:

Intent intent = new Intent(this, MyRootActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("exit", true);
startActivity(intent);

MyRootActivity should be the root activity of your task (this is the one with ACTION=MAIN and CATEGORY=LAUNCHER in the manifest).

In MyRootActivity.onCreate() add the following after super.onCreate():

if (getIntent().hasExtra("exit")) {
    // User wants to exit, so do that
   finish();
   return;
}
... here is the rest of your onCreate() code

This will only work if MyRootActivity doesn't call finish() when it starts the next Activity in your workflow. If MyRootActivity does call finish() when it starts the next Activity, you should change it so that it doesn't. In that case you will need to handle the case where the user BACKs into MyRootActivity as a special case.

Upvotes: 1

Related Questions