1 /* 2 * Copyright (C) 2014 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.example.android.supportv7.app; 18 19 import android.content.res.Configuration; 20 import android.os.Bundle; 21 import android.preference.PreferenceActivity; 22 import android.support.annotation.LayoutRes; 23 import android.support.annotation.Nullable; 24 import android.support.v7.app.ActionBar; 25 import android.support.v7.app.AppCompatDelegate; 26 import android.support.v7.widget.Toolbar; 27 import android.view.MenuInflater; 28 import android.view.View; 29 import android.view.ViewGroup; 30 31 /** 32 * A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls 33 * to be used with AppCompat. 34 * 35 * This technique can be used with an {@link android.app.Activity} class, not just 36 * {@link android.preference.PreferenceActivity}. 37 */ 38 public abstract class AppCompatPreferenceActivity extends PreferenceActivity { 39 40 private AppCompatDelegate mDelegate; 41 42 @Override onCreate(Bundle savedInstanceState)43 protected void onCreate(Bundle savedInstanceState) { 44 getDelegate().installViewFactory(); 45 getDelegate().onCreate(savedInstanceState); 46 super.onCreate(savedInstanceState); 47 } 48 49 @Override onPostCreate(Bundle savedInstanceState)50 protected void onPostCreate(Bundle savedInstanceState) { 51 super.onPostCreate(savedInstanceState); 52 getDelegate().onPostCreate(savedInstanceState); 53 } 54 getSupportActionBar()55 public ActionBar getSupportActionBar() { 56 return getDelegate().getSupportActionBar(); 57 } 58 setSupportActionBar(@ullable Toolbar toolbar)59 public void setSupportActionBar(@Nullable Toolbar toolbar) { 60 getDelegate().setSupportActionBar(toolbar); 61 } 62 63 @Override getMenuInflater()64 public MenuInflater getMenuInflater() { 65 return getDelegate().getMenuInflater(); 66 } 67 68 @Override setContentView(@ayoutRes int layoutResID)69 public void setContentView(@LayoutRes int layoutResID) { 70 getDelegate().setContentView(layoutResID); 71 } 72 73 @Override setContentView(View view)74 public void setContentView(View view) { 75 getDelegate().setContentView(view); 76 } 77 78 @Override setContentView(View view, ViewGroup.LayoutParams params)79 public void setContentView(View view, ViewGroup.LayoutParams params) { 80 getDelegate().setContentView(view, params); 81 } 82 83 @Override addContentView(View view, ViewGroup.LayoutParams params)84 public void addContentView(View view, ViewGroup.LayoutParams params) { 85 getDelegate().addContentView(view, params); 86 } 87 88 @Override onPostResume()89 protected void onPostResume() { 90 super.onPostResume(); 91 getDelegate().onPostResume(); 92 } 93 94 @Override onTitleChanged(CharSequence title, int color)95 protected void onTitleChanged(CharSequence title, int color) { 96 super.onTitleChanged(title, color); 97 getDelegate().setTitle(title); 98 } 99 100 @Override onConfigurationChanged(Configuration newConfig)101 public void onConfigurationChanged(Configuration newConfig) { 102 super.onConfigurationChanged(newConfig); 103 getDelegate().onConfigurationChanged(newConfig); 104 } 105 106 @Override onStop()107 protected void onStop() { 108 super.onStop(); 109 getDelegate().onStop(); 110 } 111 112 @Override onDestroy()113 protected void onDestroy() { 114 super.onDestroy(); 115 getDelegate().onDestroy(); 116 } 117 invalidateOptionsMenu()118 public void invalidateOptionsMenu() { 119 getDelegate().invalidateOptionsMenu(); 120 } 121 getDelegate()122 private AppCompatDelegate getDelegate() { 123 if (mDelegate == null) { 124 mDelegate = AppCompatDelegate.create(this, null); 125 } 126 return mDelegate; 127 } 128 }