Good day, I have implemented a code to detect the inactivity of the user and so far it works very well for testing I have put only one minute of inactivity after the minute has passed a query is executed that releases the quota that the inactive user has just occupied, then So far so good, but how can I make it so that the quota is also freed if the application is destroyed and the minute of inactivity passes? It only works for me if I leave the app in the background but not if I remove it completely!
//Variables
private long startTime=1*60*1000; // 15 MINS IDLE TIME
private final long interval = 1 * 1000;
CountDownTimer countDownTimer;
//Inicializo mi contador cuando la actividad se crea
countDownTimer = new MyCountDownTimer(startTime, interval);
//Implementacion
@Override
public void onUserInteraction(){
super.onUserInteraction();
//Reinicia el tiempo cada vez que el usuario interactua con la actividad
countDownTimer.cancel();
countDownTimer.start();
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
// Ejecuto mi código al pasar el minuto de inactividad
}
@Override
public void onTick(long millisUntilFinished) {
}
}
So as I mentioned this works very well but I also need it to work when the application is not only in the background but also destroyed, doing some research I found a code that was executed before the application is destroyed but it has not worked for me probably I have implemented it wrong and the use is an onFinishing
@Override
protected void onPause() {
super.onPause();
if (isFinishing()) {
Toast.makeText(getApplicationContext(), "Finalizo", Toast.LENGTH_SHORT).show();
}
}
I hope you can help me thank you very much in advance
You can't actually tell when the app is going to be killed, but you can tell when your main Activity is going to close.
To know when your Activity is going to be destroyed you can review its life cycle:
therefore when it is called
onStop()
is when the Activity is marked to be destroyed, here you could know when your main activity and therefore your app is going to finish.Another method to know when the main Activity will end is precisely using
isFinishing()
but inonPause()
, this way you can detect when the activity will end:It is not recommended to use it
onDestroy()
to know when an Activity ends since you will not be able to call processes before the Activity closes, even if you try to show aToast
, it will not work properly since the context is lost.Put it also in the Ondestroy: