Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fragment lifecycle: when "ondestroy" and "ondestroyview" are not called?

Imagine this scenario: I have a Fragment in a Pager. I try to switch to other apps, so that the Activity owning my pager (and my fragment) will be stopped and temporarily destroyed, eventually.

So, when I come back to my Activity, the Fragment's callbacks onCreate, oncreateview and so forth are called. But none of the Fragment's onDestroy callbacks were called before! It seems that after "onStop" the fragment is destroyed at once. Is it a normal behavior? Is it maybe because the Activity is destroyed without a call to its onDestroy?

like image 572
Bertuz Avatar asked Jun 19 '13 15:06

Bertuz


People also ask

Why onDestroy is not called when finished?

onDestroy() should be called at the end because then your code will be called before it is destroyed.

What is difference between onDestroy and onDestroyView?

onDestroyView() allows the fragment to clean up resources associated with its View. onDestroy() called to do final cleanup of the fragment's state. onDetach() called immediately prior to the fragment no longer being associated with its activity.

What lifecycle methods will trigger when Fragmentb replaces fragmenta?

When Fragment B replaces Fragment A,Fragment A is destroyed and Fragment B is created. However in case the transaction that had added Fragment A was saved using addToBackStack method,then backstack is holding reference to that fragment from previous transaction and hence only its view is destroyed.


1 Answers

Take a look at this question: Why implement onDestroy() if it is not guaranteed to be called?

Basically, onDestroy() is only guaranteed to be called if you call finish(). Otherwise, onDestroy() may not be called until the system deems it necessary. You might want to look at putting your "closing" logic in the onPause() or onStop() instead.

like image 57
invertigo Avatar answered Oct 04 '22 09:10

invertigo