1 /*
2  * Copyright (C) 2021 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.android.settingslib.collapsingtoolbar;
18 
19 import androidx.fragment.app.FragmentActivity;
20 import androidx.preference.PreferenceFragmentCompat;
21 
22 import com.android.settingslib.utils.BuildCompatUtils;
23 import com.android.settingslib.widget.R;
24 
25 import com.google.android.material.appbar.AppBarLayout;
26 
27 /**
28  * A base fragment that supports multi-fragments in one activity. The activity likes to switch the
29  * different fragments which extend this base fragment and must use the following code to add the
30  * fragment to stack.
31  *
32  * protected void onCreate(Bundle savedState) {
33  *    // omitted…
34  *    getFragmentManager()
35  *            .beginTransaction()
36  *            .add(R.id.content_frame, new Your_Fragment())
37  *            // Add root page to back-history
38  *            .addToBackStack( null)
39  *            .commit();
40  *    // omitted
41  * }
42  */
43 public abstract class BasePreferencesFragment extends PreferenceFragmentCompat {
44 
45     /**
46      * Gets the title which the fragment likes to show on app bar. The child class must implement
47      * this
48      * function.
49      *
50      * @return The title of the fragment will show on app bar.
51      */
getTitle()52     public abstract CharSequence getTitle();
53 
54     @Override
onResume()55     public void onResume() {
56         super.onResume();
57 
58         FragmentActivity activity = getActivity();
59         if (activity != null) {
60             activity.setTitle(getTitle());
61 
62             if (BuildCompatUtils.isAtLeastS()) {
63                 AppBarLayout appBarLayout = (AppBarLayout) activity.findViewById(R.id.app_bar);
64 
65                 if (appBarLayout != null) {
66                     appBarLayout.setExpanded(/* expanded= */ true);
67                 }
68             }
69         }
70     }
71 }
72