0

I created a basic sample application that all it does is call finish() during the Activity onStop(). Based on my understanding and from looking at other posts on Stackoverflow, this should trigger onDestroy().

My repro steps are launch the application, press the Home button on the device, which will trigger onStop(). The expected result is that it also hits onDestroy(), but I'm not seeing that.

If I call finish() during onPause() instead, it will trigger onStop() and onDestroy() as expected.

Anyone know why it would work during onPause() but not onStop()?

I've also noticed this message in the Logcat

I/ActivityManager: Activity reported stop, but no longer stopping: ActivityRecord{203ab0 u0 com.amazon.myapplication/.MainActivity t96 f}

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d("Sample", "onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
protected void onStart() {
    Log.d("Sample", "onStart");
    super.onStart();
}

@Override
protected void onResume() {
    Log.d("Sample", "onResume");
    super.onResume();
}

@Override
protected void onPause() {
    Log.d("Sample", "onPause");
    super.onPause();
}

@Override
protected void onStop() {
    Log.d("Sample", "onStop");
    finish();
    super.onStop();
}

@Override
protected void onDestroy() {
    Log.d("Sample", "onDestroy");
    super.onDestroy();
}
Brandon Lim
  • 271
  • 2
  • 12
  • Your code is working on my device. You can try placing finish() method after super.onStop() method – Rakesh Gupta Feb 13 '18 at 03:23
  • @RakeshGupta I tried placing the finish() method after super.onStop() and still wasn't able to get it to work. I've even tested this on the Android Emulator and get similar behavior. – Brandon Lim Feb 13 '18 at 18:53
  • As you can see in Logcat finish method is trying to stop activity which is already stopped. So in some device it is working differently. – Rakesh Gupta Feb 13 '18 at 19:30
  • @RakeshGupta so what is preventing onDestroy() from being triggered? – Brandon Lim Feb 13 '18 at 20:45
  • I think onDestroy() will be called only after onstop() is successfully executed by finish() which is not happening here. – Rakesh Gupta Feb 14 '18 at 02:19
  • 1
    @RakeshGupta When I run `adb shell dumpsys activity activities` I do see that the activity is in the FINISHING state, but it takes about 5 minutes till I actually see onDestroy() getting called. Seems weird why it takes so long. – Brandon Lim Feb 14 '18 at 22:37
  • Don't know much about it. I think after 5 min minutes of inactivity, android system is destroying the app's activity to reclaim memory for other purposes. – Rakesh Gupta Feb 15 '18 at 05:00
  • Check this link: https://stackoverflow.com/q/19891969/6246513 – Rakesh Gupta Feb 15 '18 at 05:07

0 Answers0