android-components / mozilla.components.lib.state.ext / observe

observe

@MainThread fun <S : State, A : Action> Store<S, A>.observe(owner: LifecycleOwner, observer: Observer<S>): Unit (source)

Registers an Observer function that will be invoked whenever the state changes. The Store.Subscription will be bound to the passed in LifecycleOwner. Once the Lifecycle state changes to DESTROYED the Observer will be unregistered automatically.

The Observer will get invoked with the current State as soon as the Lifecycle is in STARTED state.

@MainThread fun <S : State, A : Action> Store<S, A>.observe(view: <ERROR CLASS>, observer: Observer<S>): Unit (source)

Registers an Observer function that will be invoked whenever the state changes. The Store.Subscription will be bound to the passed in View. Once the View gets detached the Observer will be unregistered automatically.

Note that inside a Fragment using observe with a viewLifecycleOwner may be a better option. Only use this implementation if you have only access to a View - especially if it can exist outside of a Fragment.

The Observer will get invoked with the current State as soon as View is attached.

Once the View gets detached the Observer will get unregistered. It will NOT get automatically registered again if the same View gets attached again.