I use onDestroy() method, and my code is not completed:
@Override
public void onDestroy() {
super.onDestroy();
for (int i = 0 ; i < 10; i++) {
Toast.makeText(this, "Destroy " + i, Toast.LENGTH_SHORT).show();
}
}
The for loop stops at index 2. Why does the loop not end?
First of all the documentation says that the
onDestroy()method:and:
Also at the beginning of the docs on the Activity there is a note that the methods
onDestroy()andonStop()arekillable:And there is a recommendation to use the
onPause()method instead of those that are killable.So apparently there is some android internal timeout for the
onDestroy()function that might be controlling if nothing too long is happening in it and if it does, then it probably kills the execution. This is my guess.Based on what was in the documentation I would refrain from using this method to do what You are doing here and I would try using the
onPause()method instead.If the code is meant to be executed only when the applicatoin is finishing then use the
isFinishing()method to check for it. The docs recommend this approach: