1

I have a LocalBroadcastReceiver and I am unregistering it in my ondestroy().

Now i read about ondestroy() mentioned in these two SO answers is-ondestroy-not-always-called and why-implement-ondestroy-if-it-is-not-guaranteed-to-be-called and as well as in Androi Docs that

onDestroy will be called if you explicitly call finish();

But why in my case I am not calling finish() but still ondestroy() is getting called everytime in all of my Android devices. Also according to you guys where are the cases where ondestroy() not get called up.

Also even if Android will kill my app(due to less memory) I don't need to worry as Android is going to kill my whole app so receiver will ultimately get killed.(So there won't be any Memory Leak).

So for my use case which cases are there where ondestroy() is not going to get called up.

Sudhanshu Gaur
  • 7,486
  • 9
  • 47
  • 94

1 Answers1

2

But why in my case I am not calling finish() but still ondestroy() is getting called everytime in all of the Android devices.

The default implementation of onBackPressed() — what is triggered by the BACK button — calls finish().

where are the cases where ondestroy() not get called up.

  1. If you crash with an unhandled exception

  2. If your process is terminated in an urgent fashion (e.g., the system needs RAM to process an incoming phone call)

  3. If the user clicks "Force Stop" on your app's screen in Settings

  4. On a few devices, if the user terminates your process using a manufacturer-supplied task manager

Also even if Android will kill my app(due to less memory) I don't need to worry as Android is going to kill my whole app so receiver will ultimately get killed

Yes, because your entire process will be terminated.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • For first case if my app crashes then I don't think I have to unregister my receiver because as already app is closed and when user taps it again, again the memory will be allocated to my app. – Sudhanshu Gaur Aug 24 '17 at 17:30
  • For second case is it the same as the case I mentioned about less memory or not ?? – Sudhanshu Gaur Aug 24 '17 at 17:32
  • @SudhanshuGaur: "For first case..." -- this depends a lot on how your app is addressing unhandled exceptions. "For second case..." -- yes, this is the part of the scenario that you mentioned about Android terminating your process due to low memory conditions. Sometimes, Android will take the time to call `onDestroy()` on your running activities. Sometimes, it will not, as it cannot take the time. – CommonsWare Aug 24 '17 at 17:33
  • Are there any other cases left or not ?? because as second step android is going to cover by itself, also third and first and fourth step are very rare so I think I can use `ondestroy` ?? – Sudhanshu Gaur Aug 24 '17 at 18:45
  • 1
    @SudhanshuGaur: "Are there any other cases left or not ?" -- none that I can think of. – CommonsWare Aug 24 '17 at 23:06
  • Can you please tell me even if i have unregistered my receiver in onstop() then still it will not get unregister if app crash (first point) OR because of second point so for first point and second point where can i unregister my receiver successfully ?? – Sudhanshu Gaur Oct 03 '17 at 17:30
  • @SudhanshuGaur: I have no idea, sorry. – CommonsWare Oct 03 '17 at 17:32
  • So acc to you whenever i will press back button ondestroy() will call everytime 100 % or there are any exceptions in that too ?? – Sudhanshu Gaur Oct 03 '17 at 17:37
  • 1
    @SudhanshuGaur: The exceptions are shown in the numbered list in the question. So, you could have an unhandled exception sometime between when the user presses BACK and when the activity would ordinarily have been destroyed. Plus, pressing BACK does not necessarily destroy the activity (e.g., you overrode `onBackPressed()` to do something else, a `FragmentTransaction` is rolled back). – CommonsWare Oct 03 '17 at 17:40
  • @CommonsWare: Can you help me with any scenario wherein only onDestroy() of Activity is called? neither onPause() nor onStop() ? – Astha Garg Oct 21 '20 at 10:23
  • @AsthaGarg: "Can you help me with any scenario wherein only onDestroy() of Activity is called? neither onPause() nor onStop() ?" -- that should not be possible. If you have a project that can reproduce the problem, link to it from a separate Stack Overflow question as part of a [mcve]. – CommonsWare Oct 21 '20 at 12:30
  • @CommonsWare- I got an answer to this question, when app crashes in onCreate of Activity therein only onDestroy() is called. It was kind of an asked interview question – Astha Garg Nov 26 '20 at 13:13
  • @AsthaGarg: AFAIK, if the app crashes in `onCreate()`, `onDestroy()` will not be called. – CommonsWare Nov 26 '20 at 13:17
  • @CommonsWare oh yes, you are right. So the answer would be when we call finish() from onCreate then only onDestroy is called – Astha Garg Nov 26 '20 at 13:25
  • 1
    @AsthaGarg: Ah, good point, I did not think of that when you [originally asked the question](https://stackoverflow.com/users/8762338/astha-garg). I believe you are correct, and the activity will go from `onCreate()` to `onDestroy()` without calling the other lifecycle methods. My apologies for forgetting about that scenario! – CommonsWare Nov 26 '20 at 13:27
  • @CommonsWare : Yes..also..can you help me with https://stackoverflow.com/questions/64328837/how-to-show-your-android-app-icon-along-with-copy-paste-menus ?? (I know this is not the right way to ask to answer a question) – Astha Garg Nov 26 '20 at 13:30
  • @AsthaGarg: Sorry, I do not know what they are using for that. – CommonsWare Nov 26 '20 at 14:03
  • @CommonsWare: Oh okay no problem :) – Astha Garg Nov 26 '20 at 14:04