Context as Callbacks

when we set the listener on onAttach(context: Context) with callbacks = context as Callbacks?

does that mean we’re attaching the callback to the context? (is the context here the host Activity Context?)
or casting the context to Callback, because the host activity implemented the interface?

The context from the onAttach is always from the hosting activity. Since the activity is implementing the interface you are casting the context as an instance of the Callback.

Also, as good practice, you are removing that reference from the fragment once it is destroy to prevent memory leaks.

1 Like