Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android is there any view callback when it's destroyed?

I have a custom view component. I used it in either fragment or activity. I would like to know if there's a callback when it's destroyed from fragment/activity?

like image 405
Shumin Gao Avatar asked Jul 22 '15 23:07

Shumin Gao


People also ask

How can I tell if an Android fragment is destroyed?

Since all fragments are destroyed if the activity is destroyed, a simple answer could be calling getActivity(). isDestroyed() returning true if the activity is destroyed, therefore the fragment is destroyed.

What is on destroy in Android?

onDestroy: The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space. You can not count on onDestroy() being called.


1 Answers

View does not have a callback (except finalize(), but I don't think that's what you're asking for). View has onDetachedFromWindow() when it is removed from the screen, but this is not related to it being destroyed -- it could be attached again, which will call onAttachedToWindow().

Fragment has onDestroyView(), which may be more useful to you. Activity doesn't have an equivalent method, but you could use onDestroy() as long as you know it may never be called if the system decides to terminate your app unexpectedly.

like image 187
Karakuri Avatar answered Sep 19 '22 22:09

Karakuri