1 /*
2  * Copyright (C) 2018 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 com.android.systemui.util;
18 
19 import static androidx.lifecycle.Lifecycle.Event.ON_CREATE;
20 import static androidx.lifecycle.Lifecycle.Event.ON_DESTROY;
21 import static androidx.lifecycle.Lifecycle.Event.ON_PAUSE;
22 import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;
23 import static androidx.lifecycle.Lifecycle.Event.ON_START;
24 import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
25 
26 import android.annotation.CallSuper;
27 import android.app.Fragment;
28 import android.os.Bundle;
29 
30 import androidx.lifecycle.Lifecycle;
31 import androidx.lifecycle.LifecycleOwner;
32 import androidx.lifecycle.LifecycleRegistry;
33 
34 /**
35  * Version of {@link #Fragment} that is a {@link LifecycleOwner}.
36  */
37 public class LifecycleFragment extends Fragment implements LifecycleOwner {
38 
39     private final LifecycleRegistry mLifecycle = new LifecycleRegistry(this);
40 
getLifecycle()41     public Lifecycle getLifecycle() {
42         return mLifecycle;
43     }
44 
45     @CallSuper
46     @Override
onCreate(Bundle savedInstanceState)47     public void onCreate(Bundle savedInstanceState) {
48         mLifecycle.handleLifecycleEvent(ON_CREATE);
49         super.onCreate(savedInstanceState);
50     }
51 
52     @CallSuper
53     @Override
onStart()54     public void onStart() {
55         mLifecycle.handleLifecycleEvent(ON_START);
56         super.onStart();
57     }
58 
59     @CallSuper
60     @Override
onResume()61     public void onResume() {
62         mLifecycle.handleLifecycleEvent(ON_RESUME);
63         super.onResume();
64     }
65 
66     @CallSuper
67     @Override
onPause()68     public void onPause() {
69         mLifecycle.handleLifecycleEvent(ON_PAUSE);
70         super.onPause();
71     }
72 
73     @CallSuper
74     @Override
onStop()75     public void onStop() {
76         mLifecycle.handleLifecycleEvent(ON_STOP);
77         super.onStop();
78     }
79 
80     @CallSuper
81     @Override
onDestroy()82     public void onDestroy() {
83         mLifecycle.handleLifecycleEvent(ON_DESTROY);
84         super.onDestroy();
85     }
86 
87 }
88