blob: 9c8c1e55f3d2724eec8253ee14bdd81d0484a154 [file] [log] [blame]
Adam Powellc9fda282019-12-05 09:49:48 -08001/*
2 * Copyright 2019 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
17package androidx.lifecycle;
18
19import android.view.View;
20import android.view.ViewParent;
21
22import androidx.annotation.NonNull;
23import androidx.annotation.Nullable;
Adam Powellb3e8b512019-12-11 14:04:21 -080024import androidx.lifecycle.runtime.R;
Adam Powellc9fda282019-12-05 09:49:48 -080025
26/**
27 * Accessors for finding a view tree-local {@link LifecycleOwner} that reports the lifecycle for
28 * the given view.
29 */
30public class ViewTreeLifecycleOwner {
31 private ViewTreeLifecycleOwner() {
32 // No instances
33 }
34
35 /**
36 * Set the {@link LifecycleOwner} responsible for managing the given {@link View}.
37 * Calls to {@link #get(View)} from this view or descendants will return {@code lifecycleOwner}.
38 *
39 * <p>This should only be called by constructs such as activities or fragments that manage
40 * a view tree and reflect their own lifecycle through a {@link LifecycleOwner}. Callers
41 * should only set a {@link LifecycleOwner} that will be <em>stable.</em> The associated
42 * lifecycle should report that it is destroyed if the view tree is removed and is not
43 * guaranteed to later become reattached to a window.</p>
44 *
45 * @param view Root view managed by lifecycleOwner
46 * @param lifecycleOwner LifecycleOwner representing the manager of the given view
47 */
48 public static void set(@NonNull View view, @Nullable LifecycleOwner lifecycleOwner) {
49 view.setTag(R.id.view_tree_lifecycle_owner, lifecycleOwner);
50 }
51
52 /**
53 * Retrieve the {@link LifecycleOwner} responsible for managing the given {@link View}.
54 * This may be used to scope work or heavyweight resources associated with the view
55 * that may span cycles of the view becoming detached and reattached from a window.
56 *
57 * @param view View to fetch a {@link LifecycleOwner} for
58 * @return The {@link LifecycleOwner} responsible for managing this view and/or some subset
59 * of its ancestors
60 */
61 @Nullable
62 public static LifecycleOwner get(@NonNull View view) {
63 LifecycleOwner found = (LifecycleOwner) view.getTag(R.id.view_tree_lifecycle_owner);
64 if (found != null) return found;
65 ViewParent parent = view.getParent();
66 while (found == null && parent instanceof View) {
67 final View parentView = (View) parent;
68 found = (LifecycleOwner) parentView.getTag(R.id.view_tree_lifecycle_owner);
69 parent = parentView.getParent();
70 }
71 return found;
72 }
73}