android-components / mozilla.components.support.base.feature / ViewBoundFeatureWrapper

ViewBoundFeatureWrapper

class ViewBoundFeatureWrapper<T : LifecycleAwareFeature> (source)

Wrapper for LifecycleAwareFeature instances that keep a strong references to a View. This wrapper is helpful when the lifetime of the View may be shorter than the Lifecycle and you need to keep a reference to the LifecycleAwareFeature that may outlive the View.

ViewBoundFeatureWrapper takes care of stopping LifecycleAwareFeature and clearing references once the bound View get detached.

A common use case is a Fragment that needs to keep a reference to a LifecycleAwareFeature (e.g. to invoke onBackPressed() and the LifecycleAwareFeature holds a reference to View instances. Once the Fragment gets detached and not destroyed (e.g. when pushed to the back stack) it will still keep the reference to the LifecycleAwareFeature and therefore to the (now detached) View (-> Leak). When the Fragment gets re-attached a new View and matching LifecycleAwareFeature is getting created leading to multiple concurrent LifecycleAwareFeature and (off-screen) View instances existing in memory.

Example integration:

class MyFragment : Fragment {
    val myFeature = ViewBoundFeatureWrapper<MyFeature>()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        // Bind wrapper to feature and view. Feature will be stopped and internal reference will be cleared
        // when the view gets detached.
        myFeature.set(
            feature = MyFeature(..., view),
            owner = this,
            view = view
        )
    }

    fun doSomething() {
        // Get will return the feature instance or null if the instance was cleared (e.g. the fragment is detached)
        myFeature.get()?.doSomething()
    }

    override fun onBackPressed(): Boolean {
        return myFeature.onBackPressed()
    }
}

Constructors

Name Summary
<init> ViewBoundFeatureWrapper(feature: T, owner: LifecycleOwner, view: <ERROR CLASS>)
Convenient constructor for creating a wrapper instance and calling set immediately.ViewBoundFeatureWrapper()
Wrapper for LifecycleAwareFeature instances that keep a strong references to a View. This wrapper is helpful when the lifetime of the View may be shorter than the Lifecycle and you need to keep a reference to the LifecycleAwareFeature that may outlive the View.

Functions

Name Summary
clear fun clear(): Unit
Stops the feature and clears all internal references and observers.
get fun get(): T?
Returns the wrapped LifecycleAwareFeature or null if the View was detached and the reference was cleared.
onBackPressed fun onBackPressed(): Boolean
Convenient method for invoking UserInteractionHandler.onBackPressed on a wrapped LifecycleAwareFeature that implements UserInteractionHandler. Returns false if the LifecycleAwareFeature was cleared already.
set fun set(feature: T, owner: LifecycleOwner, view: <ERROR CLASS>): Unit
Sets the LifecycleAwareFeature reference and binds it to the Lifecycle of the LifecycleObserver as well as the View.
withFeature fun withFeature(block: (T) -> Unit): Unit
Runs the given block if this wrapper still has a reference to the LifecycleAwareFeature.

Extension Functions

Name Summary
loadResourceAsString fun Any.loadResourceAsString(path: String): String
Loads a file from the resources folder and returns its content as a string object.