blob: 315bedc168bba15415d37059fcb796eb5ec439c4 [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
18import android.app.DialogFragment;
19import android.content.Context;
20import android.support.annotation.VisibleForTesting;
21import android.view.Menu;
22import android.view.MenuInflater;
23import android.view.MenuItem;
24
25/**
26 * {@link DialogFragment} that has hooks to observe fragment lifecycle events.
27 */
28public class ObservableDialogFragment extends DialogFragment {
29
30 protected final Lifecycle mLifecycle = createLifecycle();
31
32 @Override
33 public void onAttach(Context context) {
34 super.onAttach(context);
35 mLifecycle.onAttach(context);
36 }
37
38 @Override
39 public void onStart() {
40 mLifecycle.onStart();
41 super.onStart();
42 }
43
44 @Override
45 public void onResume() {
46 mLifecycle.onResume();
47 super.onResume();
48 }
49
50 @Override
51 public void onPause() {
52 mLifecycle.onPause();
53 super.onPause();
54 }
55
56 @Override
57 public void onStop() {
58 mLifecycle.onStop();
59 super.onStop();
60 }
61
62 @Override
63 public void onDestroy() {
64 mLifecycle.onDestroy();
65 super.onDestroy();
66 }
67
68 @Override
69 public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
70 mLifecycle.onCreateOptionsMenu(menu, inflater);
71 super.onCreateOptionsMenu(menu, inflater);
72 }
73
74 @Override
75 public void onPrepareOptionsMenu(final Menu menu) {
76 mLifecycle.onPrepareOptionsMenu(menu);
77 super.onPrepareOptionsMenu(menu);
78 }
79
80 @Override
81 public boolean onOptionsItemSelected(final MenuItem menuItem) {
82 boolean lifecycleHandled = mLifecycle.onOptionsItemSelected(menuItem);
83 if (!lifecycleHandled) {
84 return super.onOptionsItemSelected(menuItem);
85 }
86 return lifecycleHandled;
87 }
88
89 @VisibleForTesting(otherwise = VisibleForTesting.NONE)
90 /** @return a new lifecycle. */
91 public static Lifecycle createLifecycle() {
92 return new Lifecycle();
93 }
94}