3

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.

Razvi
  • 400
  • 4
  • 17

1 Answers1

1

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.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • @user1269737 Why did you add this comment? This isn't necessary. I don't know why you think it is. Please create a new question if you are having specific issues. Your comment is confusing. – David Wasser Mar 31 '18 at 08:43
  • @user1269737 Not in this scenario. User's app is already running. This will not cause a "launch" `Intent` to be stored as the root of the task with the extra "exit" in it. That check is not necessary here. and it is confusing. – David Wasser Apr 02 '18 at 15:41