5.7. SettingsFragment Class

Class SettingsFragment (Fig. 5.35) extends PreferenceFragment, which provides capabilities for managing the app’s settings. Overridden method onCreate (lines 11–16) is called when the SettingsFragment is created—either by the SettingsActivity when the app is running in portrait orientation or by the MainActivity when the app is running on a tablet in landscape orientation. Line 15 uses inherited PreferenceFragment method addPreferencesFromResource to build the preferences GUI. The argument is the resource ID for the preferences.xml file you created in Section 5.4.10.


 1   // SettingsFragment.java
 2   // Subclass of PreferenceFragment for managing app settings
 3   package com.deitel.flagquiz;
 4
 5   import android.os.Bundle;
 6   import android.preference.PreferenceFragment;
 7
 8   public class SettingsFragment extends PreferenceFragment
 9   {
10      // creates preferences GUI from preferences.xml file in res/xml
11      @Override
12      public void onCreate(Bundle savedInstanceState)
13      {
14         super.onCreate(savedInstanceState);
15         addPreferencesFromResource(R.xml.preferences); // load from XML
16      }
17   } // end class SettingsFragment


Fig. 5.35 | Subclass of PreferenceFragment for managing app settings.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.76.218