I have an application which I want to be able to finish completely from any activity and that it does not remain in the background. The application will consist of 5 Activity's. In the third activity I have the following code.
@Override
protected void onDestroy() {
Log.i(TAG,"Me destrulleron");
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());
}
@Override
public void onBackPressed() {
if(this.tiempoPrimerClick + INTERVALO > System.currentTimeMillis()){
finish();
return;
}else{
Toast.makeText(this,"Vuelve a precionar para salir", Toast.LENGTH_SHORT).show();
}
tiempoPrimerClick = System.currentTimeMillis();
}
Which ends the third Activity and the second Activity but leaves me in the MainActivity.
In the second activity I have the following code
@Override
protected void onDestroy() {
Log.i(TAG,"Me destrulleron");
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());
} @Override
public void onBackPressed() {
if(this.tiempoPrimerClick + INTERVALO > System.currentTimeMillis()){
finish();
return;
}else{
Toast.makeText(this,"Vuelve a presionar para salir", Toast.LENGTH_SHORT).show();
}
tiempoPrimerClick = System.currentTimeMillis();
}
Being in this activity if the application is closed and the process is terminated.
Now in the MainActivity I have the following code:
@Override
protected void onDestroy() {
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());
}
I am still checking why in the third activity the application does not close completely. It would be a great help if you give me a hand.
From version
>= API 16
you can usefinishAffinity()
but there is an alternative that works on>= API 11
Alternative method
The method is to load one
Actividad
on top of all the others, deleting its stack, and then closing it for good.Every time you want to leave all the
Actividades
open ones, to the eventonBackPressed
of all of them putand in the
MainActivity.java
capture if there is aintent
with value of inEXIT
order to finish the application in its entirety.Adapted from SO and SO answers
Test
finishAffinity();
It should be noted that it corresponds to the Jelly Bean API and if it gives you an error, place
above the function where you are using the above.