27

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?

Suragch
  • 364,799
  • 232
  • 1,155
  • 1,198
Shumin Gao
  • 547
  • 2
  • 7
  • 14

2 Answers2

31

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.

Karakuri
  • 36,506
  • 12
  • 75
  • 103
  • does that mean that `onDestroyView()` will be called when system decides to terminate my app unexpectedly? – Android developer Nov 06 '16 at 14:13
  • @RadekKłos Likely not. If what you actually want is to know if the View is currently on screen, perhaps a better callback is `View.onDetachedFromWindow()` – Karakuri Nov 06 '16 at 19:14
  • Thank you for answer. I guess that `onDetachedFromWindow()` still won't be called when system decides to terminate me unexpectedly, but it will be called along with, for example, `onDestroy()` of Activity? Is it right? What I want to achieve is I want to store view's personal data to SharedPreferences when app terminates and I want to do this in this View object, not Activity that has this view in its layout. – Android developer Nov 07 '16 at 09:24
  • 2
    @RadekKłos `onDetachedFromWindow()` is called when the View is no longer attached to its window, meaning it has no surface for drawing. This should happen anytime the Activity is no longer visible to the user, which is probably long before the system decides to terminate your Activity. – Karakuri Nov 08 '16 at 07:16
-1

Thanks for Karakuri answer, for optional solution (use simple callback)

Note

view OnLayoutChangeListener not called before view detached from window

   view.listener = object :OnViewAttachedChangeListener{
                    override fun onAttachedFromWindow(view: View, isAttached: Boolean) {
    
                    }
                }

Add simple callback for tracking attachment state.

   internal class AttachedView(context: Context): View(context){

        internal var listener: OnViewAttachedChangeListener?= null
            get() = field

        override fun onDetachedFromWindow() {
            super.onDetachedFromWindow()
            notifyOnAttachedToWindow(false)
        }

        override fun onAttachedToWindow() {
            super.onAttachedToWindow()
            notifyOnAttachedToWindow(true)
        }

        private fun notifyOnAttachedToWindow(isAttached: Boolean){
            listener?.onAttachedFromWindow(this, isAttached)
        }
    }

    internal interface OnViewAttachedChangeListener{
        fun onAttachedFromWindow(view: View, isAttached: Boolean)
    }
Community
  • 1
  • 1
Vahe Gharibyan
  • 2,725
  • 1
  • 21
  • 30