SubcomposeLayoutState.PausedPrecomposition


A PausedPrecomposition is a subcomposition that can be composed incrementally as it supports being paused and resumed.

Pausable subcomposition can be used between frames to prepare a subcomposition before it is required by the main composition. For example, this is used in lazy lists to prepare list items in between frames to that are likely to be scrolled in. The composition is paused when the start of the next frame is near, allowing composition to be spread across multiple frames without delaying the production of the next frame.

Summary

Public functions

SubcomposeLayoutState.PrecomposedSlotHandle

Apply the composition.

Cmn
Unit

Cancels the paused composition.

Cmn
Boolean

Resume the composition that has been paused.

Cmn

Public properties

Boolean

Returns true when the PausedPrecomposition is complete.

Cmn

Public functions

apply

fun apply(): SubcomposeLayoutState.PrecomposedSlotHandle

Apply the composition. This is the last step of a paused composition and is required to be called prior to the composition is usable.

Calling apply should always be proceeded with a check of isComplete before it is called and potentially calling resume in a loop until isComplete returns true. This can happen if resume returned true but apply was not synchronously called immediately afterwords. Any state that was read that changed between when resume being called and apply being called may require the paused composition to be resumed before applied.

Returns
SubcomposeLayoutState.PrecomposedSlotHandle

PrecomposedSlotHandle you can use to premeasure the slot as well, or to dispose the composed content.

cancel

fun cancel(): Unit

Cancels the paused composition. This should only be used if the composition is going to be disposed and the entire composition is not going to be used.

resume

fun resume(shouldPause: ShouldPauseCallback): Boolean

Resume the composition that has been paused. This method should be called until resume returns true or isComplete is true which has the same result as the last result of calling resume. The shouldPause parameter is a lambda that returns whether the composition should be paused. For example, in lazy lists this returns false until just prior to the next frame starting in which it returns true

Calling resume after it returns true or when isComplete is true will throw an exception.

Parameters
shouldPause: ShouldPauseCallback

A lambda that is used to determine if the composition should be paused. This lambda is called often so should be a very simple calculation. Returning true does not guarantee the composition will pause, it should only be considered a request to pause the composition. Not all composable functions are pausable and only pausable composition functions will pause.

Returns
Boolean

true if the composition is complete and false if one or more calls to resume are required to complete composition.

Public properties

isComplete

val isCompleteBoolean

Returns true when the PausedPrecomposition is complete. isComplete matches the last value returned from resume. Once a PausedPrecomposition is isComplete the apply method should be called. If the apply method is not called synchronously and immediately after resume returns true then this isComplete can return false as any state changes read by the paused composition while it is paused will cause the composition to require the paused composition to need to be resumed before it is used.