Home | History | Annotate | Download | only in lifecycle
      1 /*
      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  */
     16 package com.android.settingslib.core.lifecycle;
     17 
     18 
     19 import android.annotation.CallSuper;
     20 import android.content.Context;
     21 import android.os.Bundle;
     22 import android.support.v14.preference.PreferenceFragment;
     23 import android.support.v7.preference.PreferenceScreen;
     24 import android.view.Menu;
     25 import android.view.MenuInflater;
     26 import android.view.MenuItem;
     27 
     28 /**
     29  * {@link PreferenceFragment} that has hooks to observe fragment lifecycle events.
     30  */
     31 public abstract class ObservablePreferenceFragment extends PreferenceFragment {
     32 
     33     private final Lifecycle mLifecycle = new Lifecycle();
     34 
     35     protected Lifecycle getLifecycle() {
     36         return mLifecycle;
     37     }
     38 
     39     @CallSuper
     40     @Override
     41     public void onAttach(Context context) {
     42         super.onAttach(context);
     43         mLifecycle.onAttach(context);
     44     }
     45 
     46     @CallSuper
     47     @Override
     48     public void onCreate(Bundle savedInstanceState) {
     49         mLifecycle.onCreate(savedInstanceState);
     50         super.onCreate(savedInstanceState);
     51     }
     52 
     53     @Override
     54     public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
     55         mLifecycle.setPreferenceScreen(preferenceScreen);
     56         super.setPreferenceScreen(preferenceScreen);
     57     }
     58 
     59     @CallSuper
     60     @Override
     61     public void onSaveInstanceState(Bundle outState) {
     62         super.onSaveInstanceState(outState);
     63         mLifecycle.onSaveInstanceState(outState);
     64     }
     65 
     66     @CallSuper
     67     @Override
     68     public void onStart() {
     69         mLifecycle.onStart();
     70         super.onStart();
     71     }
     72 
     73     @CallSuper
     74     @Override
     75     public void onStop() {
     76         mLifecycle.onStop();
     77         super.onStop();
     78     }
     79 
     80     @CallSuper
     81     @Override
     82     public void onResume() {
     83         mLifecycle.onResume();
     84         super.onResume();
     85     }
     86 
     87     @CallSuper
     88     @Override
     89     public void onPause() {
     90         mLifecycle.onPause();
     91         super.onPause();
     92     }
     93 
     94     @CallSuper
     95     @Override
     96     public void onDestroy() {
     97         mLifecycle.onDestroy();
     98         super.onDestroy();
     99     }
    100 
    101     @CallSuper
    102     @Override
    103     public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
    104         mLifecycle.onCreateOptionsMenu(menu, inflater);
    105         super.onCreateOptionsMenu(menu, inflater);
    106     }
    107 
    108     @CallSuper
    109     @Override
    110     public void onPrepareOptionsMenu(final Menu menu) {
    111         mLifecycle.onPrepareOptionsMenu(menu);
    112         super.onPrepareOptionsMenu(menu);
    113     }
    114 
    115     @CallSuper
    116     @Override
    117     public boolean onOptionsItemSelected(final MenuItem menuItem) {
    118         boolean lifecycleHandled = mLifecycle.onOptionsItemSelected(menuItem);
    119         if (!lifecycleHandled) {
    120             return super.onOptionsItemSelected(menuItem);
    121         }
    122         return lifecycleHandled;
    123     }
    124 }
    125