Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
831 views
in Technique[技术] by (71.8m points)

android - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...