RetainScope

Class

Common
public abstract class RetainScope : RetainStateProvider

A RetainScope acts as a storage area for objects being retained. An instance of a RetainScope also defines a specific retention policy to describe when removed state should be retained and when it should be forgotten.

The general pattern for retention is as follows:

  1. The RetainScope receives a notification from that transient content removal is about to begin. The source of this notification varies depending on what retention scenario is being captured, but could, for example, be a signal that an Android Activity is being recreated, or that content is about to be navigated away from/collapsed with the potential of being returned to. At this time, the scope's owner should call requestKeepExitedValues.
  2. Transient content removal begins. The content is recomposed, removed from the hierarchy, and remembered values are forgotten. Values remembered by retain leave the composition but are not yet released. Every value returned by retain will be passed as an argument to saveExitingValue so that it can later be returned by getExitedValueOrDefault.
  3. An arbitrary amount of time passes, and the removed content is restored in the composition hierarchy at its previous location. When a retain call is invoked during the restoration, it calls getExitedValueOrDefault. If all the input keys match a retained value, the previous result is returned and the retained value is removed from the pool of restorable objects that exited the previous composition. This step may be skipped if it becomes impossible to return to the transiently removed content while this scope is keeping exited values.
  4. The content finishes composing after being restored, and the entire frame completes. The owner of this scope should call unRequestKeepExitedValues. When retention stops being requested, it immediately ends. Any values that are retained and not currently used in a composition (and therefore not restored by getExitedValueOrDefault) are then immediately discarded.

A given RetainScope should only be used by a single Recomposer at a time. It can move between recomposers (for example, when the Window is recreated), but should never be used by two Recomposers simultaneously. It is valid for a RetainScope to be used in multiple compositions at the same time, or in the same composition multiple times.

Functions

public abstract fun getExitedValueOrDefault(key: Any, defaultIfAbsent: Any?): Any?

If this scope is currently keeping exited values and has a value previously created with the given keys, its original record is returned and removed from the list of exited kept objects that this scope is tracking.

Parameters

keyThe keys to resolve a retained value that has left composition
defaultIfAbsentA value to be returned if there are no retained values that have exited composition and are being held by this RetainScope for the given keys.

Returns

A retained value for keys if there is one and it hasn't already re-entered composition, otherwise defaultIfAbsent.