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
17package com.example.android.supportpreference;
18
19import android.os.Bundle;
20
21import androidx.appcompat.app.AppCompatActivity;
22import androidx.fragment.app.Fragment;
23import androidx.preference.PreferenceFragmentCompat;
24import androidx.preference.PreferenceScreen;
25
26/**
27 * Demonstration of PreferenceFragment, showing a single fragment in an
28 * activity.
29 */
30public class FragmentSupportPreferencesCompat extends AppCompatActivity
31        implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
32
33    @Override
34    protected void onCreate(Bundle savedInstanceState) {
35        super.onCreate(savedInstanceState);
36
37        // Display the fragment as the main content.
38        if (savedInstanceState == null) {
39            getSupportFragmentManager().beginTransaction().replace(android.R.id.content,
40                    new PrefsFragment()).commit();
41        }
42    }
43
44    @Override
45    public boolean onPreferenceStartScreen(PreferenceFragmentCompat caller, PreferenceScreen pref) {
46        final Fragment f = new PrefsFragment();
47        final Bundle args = new Bundle(1);
48        args.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, pref.getKey());
49        f.setArguments(args);
50        getSupportFragmentManager().beginTransaction()
51                .replace(android.R.id.content, f)
52                .addToBackStack(null)
53                .commit();
54        return true;
55    }
56
57    //BEGIN_INCLUDE(support_fragment_compat)
58    public static class PrefsFragment extends PreferenceFragmentCompat {
59
60        @Override
61        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
62            // Load the preferences from an XML resource
63            setPreferencesFromResource(R.xml.preferences, rootKey);
64        }
65    }
66//END_INCLUDE(support_fragment_compat)
67}
68