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