blob: dc95384b26a50aa9e95ac568780fc04b44ecdbac [file] [log] [blame]
Juan Lang5efe8f02017-05-04 16:59:46 -07001/*
2 * Copyright (C) 2016 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 */
16package com.android.settingslib.core.lifecycle;
17
Tony Mantlerece840b2017-11-10 13:16:59 -080018import static android.arch.lifecycle.Lifecycle.Event.ON_CREATE;
19import static android.arch.lifecycle.Lifecycle.Event.ON_DESTROY;
20import static android.arch.lifecycle.Lifecycle.Event.ON_PAUSE;
21import static android.arch.lifecycle.Lifecycle.Event.ON_RESUME;
22import static android.arch.lifecycle.Lifecycle.Event.ON_START;
23import static android.arch.lifecycle.Lifecycle.Event.ON_STOP;
24
Juan Lang5efe8f02017-05-04 16:59:46 -070025import android.app.DialogFragment;
Tony Mantlerece840b2017-11-10 13:16:59 -080026import android.arch.lifecycle.LifecycleOwner;
Juan Lang5efe8f02017-05-04 16:59:46 -070027import android.content.Context;
Tony Mantlerece840b2017-11-10 13:16:59 -080028import android.os.Bundle;
Juan Lang5efe8f02017-05-04 16:59:46 -070029import android.view.Menu;
30import android.view.MenuInflater;
31import android.view.MenuItem;
32
33/**
34 * {@link DialogFragment} that has hooks to observe fragment lifecycle events.
35 */
Tony Mantlerece840b2017-11-10 13:16:59 -080036public class ObservableDialogFragment extends DialogFragment implements LifecycleOwner {
Juan Lang5efe8f02017-05-04 16:59:46 -070037
Tony Mantlerece840b2017-11-10 13:16:59 -080038 protected final Lifecycle mLifecycle = new Lifecycle(this);
Juan Lang5efe8f02017-05-04 16:59:46 -070039
40 @Override
41 public void onAttach(Context context) {
42 super.onAttach(context);
43 mLifecycle.onAttach(context);
44 }
45
46 @Override
Tony Mantlerece840b2017-11-10 13:16:59 -080047 public void onCreate(Bundle savedInstanceState) {
48 mLifecycle.onCreate(savedInstanceState);
49 mLifecycle.handleLifecycleEvent(ON_CREATE);
50 super.onCreate(savedInstanceState);
51 }
52
53 @Override
Juan Lang5efe8f02017-05-04 16:59:46 -070054 public void onStart() {
Tony Mantlerece840b2017-11-10 13:16:59 -080055 mLifecycle.handleLifecycleEvent(ON_START);
Juan Lang5efe8f02017-05-04 16:59:46 -070056 super.onStart();
57 }
58
59 @Override
60 public void onResume() {
Tony Mantlerece840b2017-11-10 13:16:59 -080061 mLifecycle.handleLifecycleEvent(ON_RESUME);
Juan Lang5efe8f02017-05-04 16:59:46 -070062 super.onResume();
63 }
64
65 @Override
66 public void onPause() {
Tony Mantlerece840b2017-11-10 13:16:59 -080067 mLifecycle.handleLifecycleEvent(ON_PAUSE);
Juan Lang5efe8f02017-05-04 16:59:46 -070068 super.onPause();
69 }
70
71 @Override
72 public void onStop() {
Tony Mantlerece840b2017-11-10 13:16:59 -080073 mLifecycle.handleLifecycleEvent(ON_STOP);
Juan Lang5efe8f02017-05-04 16:59:46 -070074 super.onStop();
75 }
76
77 @Override
78 public void onDestroy() {
Tony Mantlerece840b2017-11-10 13:16:59 -080079 mLifecycle.handleLifecycleEvent(ON_DESTROY);
Juan Lang5efe8f02017-05-04 16:59:46 -070080 super.onDestroy();
81 }
82
83 @Override
84 public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
85 mLifecycle.onCreateOptionsMenu(menu, inflater);
86 super.onCreateOptionsMenu(menu, inflater);
87 }
88
89 @Override
90 public void onPrepareOptionsMenu(final Menu menu) {
91 mLifecycle.onPrepareOptionsMenu(menu);
92 super.onPrepareOptionsMenu(menu);
93 }
94
95 @Override
96 public boolean onOptionsItemSelected(final MenuItem menuItem) {
97 boolean lifecycleHandled = mLifecycle.onOptionsItemSelected(menuItem);
98 if (!lifecycleHandled) {
99 return super.onOptionsItemSelected(menuItem);
100 }
101 return lifecycleHandled;
102 }
103
Tony Mantlerece840b2017-11-10 13:16:59 -0800104 @Override
105 public Lifecycle getLifecycle() {
106 return mLifecycle;
Juan Lang5efe8f02017-05-04 16:59:46 -0700107 }
108}