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