1 /* 2 * Copyright 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.compose.runtime 18 19 /** 20 * Observes lifecycle of the node emitted with [ReusableComposeNode] or [ComposeNode] inside 21 * [ReusableContentHost] and [ReusableContent]. 22 * 23 * The [ReusableContentHost] introduces the concept of reusing (or recycling) nodes, as well as 24 * deactivating parts of composition, while keeping the nodes around to reuse common structures in 25 * the next iteration. In this state, [RememberObserver] is not sufficient to track lifetime of data 26 * associated with reused node, as deactivated or reused parts of composition is disposed. 27 * 28 * These callbacks track intermediate states of the node in reusable groups for managing data 29 * contained inside reusable nodes or associated with them (e.g. subcomposition). 30 * 31 * Important: the runtime only supports node implementation of this interface. 32 */ 33 interface ComposeNodeLifecycleCallback { 34 /** 35 * Invoked when the node was reused in the composition. Consumers might use this callback to 36 * reset data associated with the previous content, as it is no longer valid. 37 */ onReusenull38 fun onReuse() 39 40 /** 41 * Invoked when the group containing the node was deactivated. This happens when the content of 42 * [ReusableContentHost] is deactivated. 43 * 44 * The node will not be reused in this recompose cycle, but might be reused or released in the 45 * future. Consumers might use this callback to release expensive resources or stop continuous 46 * process that was dependent on the node being used in composition. 47 * 48 * If the node is reused, [onReuse] will be called again to prepare the node for reuse. 49 * Similarly, [onRelease] will indicate that deactivated node will never be reused again. 50 */ 51 fun onDeactivate() 52 53 /** 54 * Invoked when the node exits the composition entirely and won't be reused again. All 55 * intermediate data related to the node can be safely disposed. 56 */ 57 fun onRelease() 58 } 59