-2

I have a line of code that I should call after my activity is destroyed, so I override onDestroy() method and I call it inside. Like this:

   @override
   public void onDestroy(){

     //my code here

     super.onDestroy()  

   }

Now I noticed that the this line of code is not always executed when my activity finishes.

I read about this and some said don't depend on onDestroy() method to call something.

My question is I need to call the code from onDestroy() and I want it to always work. Any thoughts on this? And why onDestroy() is found in the first place if we cant depend on it to execute?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22
  • check this links why onDestroy is not always called. https://stackoverflow.com/questions/19608948/is-ondestroy-not-always-called – androholic Apr 01 '18 at 17:08

4 Answers4

1

It is not a good idea to depend on onDestroy() for your cleanup works. It is better to perform these operation in onPause() or onStop(). These are the callback methods which the system calls whenever your activity goes out of view.

Ezio
  • 2,837
  • 2
  • 29
  • 45
  • But I have to call the code only when my activity is finished. Not when it is out of view. – Hasan Bou Taam Apr 01 '18 at 16:54
  • This will serve your purpose, whatever is it that you are trying to clear in onDestroy, you can reinitialise it in onResume(). There is no way to explicitly call onDestroy. It is a system call and android does it whenever it needs to clear some memory. – Ezio Apr 01 '18 at 16:56
  • I initialize a listener in onCreate and remove it in onDestroy. I have to do it this way. – Hasan Bou Taam Apr 01 '18 at 16:58
  • I cant reinitialize. – Hasan Bou Taam Apr 01 '18 at 16:58
  • listeners are ideally initialised in onResume and cleared it in onPause. I dont understand why you can't do that. – Ezio Apr 01 '18 at 16:59
  • I call a method from onCreate, now when I resume my activity I dont want this method to be called so I cant put the method in onStart() or in onResume(). – Hasan Bou Taam Apr 01 '18 at 17:01
  • And I must clean in onDestroy(). – Hasan Bou Taam Apr 01 '18 at 17:02
  • I think I understood what you are trying to do. If I understand correctly, the correct way to do it is use a flag. Set that flag to true once your method in onResume is called.Put a check in onResume to see the status of the flag before calling your listener. Initialise that flag in onCreate and clear it in onDestroy. – Ezio Apr 01 '18 at 17:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168015/discussion-between-ezio-and-svi-data). – Ezio Apr 02 '18 at 02:46
1

Now I noticed that the this line of code is not always executed when my activity finishes.

Either:

  • onDestroy() is called, or

  • You crashed with an unhandled exception, or

  • Your process was terminated (e.g., user revoked a runtime permission from Settings, Android needed RAM in a hurry)

I read about this and some said don't depend on onDestroy() method to call something.

Correct. onDestroy() is for cleanup of things assuming that your app is continuing to run normally. However, if your app is no longer running normally (you crashed) or your app is gone (process terminated), onDestroy() is not relevant.

I need to call the code from onDestroy() and I want it to always work. Any thoughts on this?

You will need to reconsider those two requirements, as they cannot both be true. Either initialize in onStart() and clean up in onStop(), or live with the fact that your cleanup code may not be called. Even the onStop() scenario is not completely guaranteed — in a multi-window environment, the user could revoke a runtime permission in Settings while your app is still visible — but we're getting into fairly unlikely scenarios.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So onDestroy() is called only when I finish my activity in a clean way. And it won't be called when the activity finishes it self due to a crash. – Hasan Bou Taam Apr 01 '18 at 18:48
  • So if onDestroy is not guaranteed then why does it exist any way. – Hasan Bou Taam Apr 01 '18 at 18:49
  • @svi.data: Mostly, it is there to clean up stuff that might result in memory leaks (e.g., dangling threads). If your process is being terminated, the memory goes away and so there are no more leaks to consider. If your app crashed, you have bigger problems than a memory leak. And in all other cases, `onDestroy()` gets called. – CommonsWare Apr 01 '18 at 19:17
  • 1
    @CommonsWare, re *"And in all other cases [everywhere], `onDestroy()` gets called"* ??? Didn't the documentation explicitly said No to that. – Pacerier May 09 '20 at 15:38
  • @Pacerier: If you have a link that disagrees with the three bullets at the top of my answer, I'd love to see it! – CommonsWare May 09 '20 at 15:39
  • @CommonsWare, Separately, re *"we're getting into fairly unlikely scenarios"*; You're aware that onDestroy is (almost) **never** called when the user clears the activity off the back stack right? as tested on my system here. – Pacerier May 09 '20 at 16:48
  • @Pacerier: "when the user clears the activity off the back stack right?" -- if you mean the user presses BACK, `onDestroy()` is always called unless you are taking steps to prevent the activity from honoring it (e.g., override `onBackPressed()`. If you mean swipe the app off the overview screen, your mileage may vary depending on device manufacturer and OS version. – CommonsWare May 09 '20 at 16:56
  • @CommonsWare, yea point being it's very common for onDestroy not to be called too. – Pacerier May 09 '20 at 17:19
  • @Pacerier: Everything that you have described so far fits the three bullets at the top of my answer. Swiping the app off the overview screen, for example, if it does not trigger `onDestroy()`, is because the process was simply terminated immediately. If you override `onBackPressed()` and suppress the default `finish()` behavior, `onDestroy()` will not be called at that point in time, but it might at a later point (e.g., app code itself calls `finish()`). – CommonsWare May 09 '20 at 17:22
  • @CommonsWare, yep the bullets cover all bases... Your point is that missing `onDestroy` is exceptional. My point is I never count on `(Application)getApplicationContext()` to ever work. – Pacerier May 10 '20 at 15:03
0

There is no guarantee onDestroy it will ever be called, but i had rely on onPause to check if the Activity is finishing by calling if( isFinishing() ) // activity is about to finish yet this might not work properly.

I had rethink of my logic to not rely onDestroy at all.

boolean isFinishing ()

Check to see whether this activity is in the process of finishing, either because you called finish() on it or someone else has requested that it finished. This is often used in onPause() to determine whether the activity is simply pausing or completely finishing.

https://developer.android.com/reference/android/app/Activity.html#isFinishing%28%29

Kosh
  • 6,140
  • 3
  • 36
  • 67
0

These are the system calls for the method they will crash when they are called explicitly by the programmer...

neelkanth_vyas
  • 192
  • 1
  • 7