AppCompatPreferenceActivity.java revision b31c3281d870e9abb673db239234d580dcc4feff
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
17package com.example.android.supportv7.app;
18
19import android.content.res.Configuration;
20import android.os.Bundle;
21import android.preference.PreferenceActivity;
22import androidx.annotation.LayoutRes;
23import androidx.annotation.Nullable;
24import androidx.appcompat.app.ActionBar;
25import androidx.appcompat.app.AppCompatDelegate;
26import androidx.appcompat.widget.Toolbar;
27import android.view.MenuInflater;
28import android.view.View;
29import 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 */
38public abstract class AppCompatPreferenceActivity extends PreferenceActivity {
39
40    private AppCompatDelegate mDelegate;
41
42    @Override
43    protected void onCreate(Bundle savedInstanceState) {
44        getDelegate().installViewFactory();
45        getDelegate().onCreate(savedInstanceState);
46        super.onCreate(savedInstanceState);
47    }
48
49    @Override
50    protected void onPostCreate(Bundle savedInstanceState) {
51        super.onPostCreate(savedInstanceState);
52        getDelegate().onPostCreate(savedInstanceState);
53    }
54
55    public ActionBar getSupportActionBar() {
56        return getDelegate().getSupportActionBar();
57    }
58
59    public void setSupportActionBar(@Nullable Toolbar toolbar) {
60        getDelegate().setSupportActionBar(toolbar);
61    }
62
63    @Override
64    public MenuInflater getMenuInflater() {
65        return getDelegate().getMenuInflater();
66    }
67
68    @Override
69    public void setContentView(@LayoutRes int layoutResID) {
70        getDelegate().setContentView(layoutResID);
71    }
72
73    @Override
74    public void setContentView(View view) {
75        getDelegate().setContentView(view);
76    }
77
78    @Override
79    public void setContentView(View view, ViewGroup.LayoutParams params) {
80        getDelegate().setContentView(view, params);
81    }
82
83    @Override
84    public void addContentView(View view, ViewGroup.LayoutParams params) {
85        getDelegate().addContentView(view, params);
86    }
87
88    @Override
89    protected void onPostResume() {
90        super.onPostResume();
91        getDelegate().onPostResume();
92    }
93
94    @Override
95    protected void onTitleChanged(CharSequence title, int color) {
96        super.onTitleChanged(title, color);
97        getDelegate().setTitle(title);
98    }
99
100    @Override
101    public void onConfigurationChanged(Configuration newConfig) {
102        super.onConfigurationChanged(newConfig);
103        getDelegate().onConfigurationChanged(newConfig);
104    }
105
106    @Override
107    protected void onStop() {
108        super.onStop();
109        getDelegate().onStop();
110    }
111
112    @Override
113    protected void onDestroy() {
114        super.onDestroy();
115        getDelegate().onDestroy();
116    }
117
118    @Override
119    public void invalidateOptionsMenu() {
120        getDelegate().invalidateOptionsMenu();
121    }
122
123    private AppCompatDelegate getDelegate() {
124        if (mDelegate == null) {
125            mDelegate = AppCompatDelegate.create(this, null);
126        }
127        return mDelegate;
128    }
129}