PreferenceActivity.java revision 291905e34a7f0ae03c68fb2c1b8c34b92d447fbf
1/* 2 * Copyright (C) 2007 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 android.preference; 18 19import com.android.internal.util.XmlUtils; 20 21import org.xmlpull.v1.XmlPullParser; 22import org.xmlpull.v1.XmlPullParserException; 23 24import android.app.Fragment; 25import android.app.ListActivity; 26import android.content.Context; 27import android.content.Intent; 28import android.content.res.Configuration; 29import android.content.res.TypedArray; 30import android.content.res.XmlResourceParser; 31import android.graphics.drawable.Drawable; 32import android.os.Bundle; 33import android.os.Handler; 34import android.os.Message; 35import android.text.TextUtils; 36import android.util.AttributeSet; 37import android.util.Log; 38import android.util.Xml; 39import android.view.LayoutInflater; 40import android.view.View; 41import android.view.ViewGroup; 42import android.view.View.OnClickListener; 43import android.widget.ArrayAdapter; 44import android.widget.Button; 45import android.widget.ImageView; 46import android.widget.ListView; 47import android.widget.TextView; 48 49import java.io.IOException; 50import java.util.ArrayList; 51import java.util.List; 52 53/** 54 * This is the base class for an activity to show a hierarchy of preferences 55 * to the user. Prior to {@link android.os.Build.VERSION_CODES#HONEYCOMB} 56 * this class only allowed the display of a single set of preference; this 57 * functionality should now be found in the new {@link PreferenceFragment} 58 * class. If you are using PreferenceActivity in its old mode, the documentation 59 * there applies to the deprecated APIs here. 60 * 61 * <p>This activity shows one or more headers of preferences, each of with 62 * is associated with a {@link PreferenceFragment} to display the preferences 63 * of that header. The actual layout and display of these associations can 64 * however vary; currently there are two major approaches it may take: 65 * 66 * <ul> 67 * <li>On a small screen it may display only the headers as a single list 68 * when first launched. Selecting one of the header items will re-launch 69 * the activity with it only showing the PreferenceFragment of that header. 70 * <li>On a large screen in may display both the headers and current 71 * PreferenceFragment together as panes. Selecting a header item switches 72 * to showing the correct PreferenceFragment for that item. 73 * </ul> 74 * 75 * <p>Subclasses of PreferenceActivity should implement 76 * {@link #onBuildHeaders} to populate the header list with the desired 77 * items. Doing this implicitly switches the class into its new "headers 78 * + fragments" mode rather than the old style of just showing a single 79 * preferences list. 80 * 81 * <a name="SampleCode"></a> 82 * <h3>Sample Code</h3> 83 * 84 * <p>The following sample code shows a simple preference activity that 85 * has two different sets of preferences. The implementation, consisting 86 * of the activity itself as well as its two preference fragments is:</p> 87 * 88 * {@sample development/samples/ApiDemos/src/com/example/android/apis/preference/PreferenceWithHeaders.java 89 * activity} 90 * 91 * <p>The preference_headers resource describes the headers to be displayed 92 * and the fragments associated with them. It is: 93 * 94 * {@sample development/samples/ApiDemos/res/xml/preference_headers.xml headers} 95 * 96 * <p>The first header is shown by Prefs1Fragment, which populates itself 97 * from the following XML resource:</p> 98 * 99 * {@sample development/samples/ApiDemos/res/xml/fragmented_preferences.xml preferences} 100 * 101 * <p>Note that this XML resource contains a preference screen holding another 102 * fragment, the Prefs1FragmentInner implemented here. This allows the user 103 * to traverse down a hierarchy of preferences; pressing back will pop each 104 * fragment off the stack to return to the previous preferences. 105 * 106 * <p>See {@link PreferenceFragment} for information on implementing the 107 * fragments themselves. 108 */ 109public abstract class PreferenceActivity extends ListActivity implements 110 PreferenceManager.OnPreferenceTreeClickListener, 111 PreferenceFragment.OnPreferenceStartFragmentCallback { 112 private static final String TAG = "PreferenceActivity"; 113 114 private static final String PREFERENCES_TAG = "android:preferences"; 115 116 /** 117 * When starting this activity, the invoking Intent can contain this extra 118 * string to specify which fragment should be initially displayed. 119 */ 120 public static final String EXTRA_SHOW_FRAGMENT = ":android:show_fragment"; 121 122 /** 123 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT}, 124 * this extra can also be specify to supply a Bundle of arguments to pass 125 * to that fragment when it is instantiated during the initial creation 126 * of PreferenceActivity. 127 */ 128 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":android:show_fragment_args"; 129 130 /** 131 * When starting this activity, the invoking Intent can contain this extra 132 * boolean that the header list should not be displayed. This is most often 133 * used in conjunction with {@link #EXTRA_SHOW_FRAGMENT} to launch 134 * the activity to display a specific fragment that the user has navigated 135 * to. 136 */ 137 public static final String EXTRA_NO_HEADERS = ":android:no_headers"; 138 139 private static final String BACK_STACK_PREFS = ":android:prefs"; 140 141 // extras that allow any preference activity to be launched as part of a wizard 142 143 // show Back and Next buttons? takes boolean parameter 144 // Back will then return RESULT_CANCELED and Next RESULT_OK 145 private static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar"; 146 147 // add a Skip button? 148 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip"; 149 150 // specify custom text for the Back or Next buttons, or cause a button to not appear 151 // at all by setting it to null 152 private static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text"; 153 private static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text"; 154 155 // --- State for new mode when showing a list of headers + prefs fragment 156 157 private final ArrayList<Header> mHeaders = new ArrayList<Header>(); 158 159 private HeaderAdapter mAdapter; 160 161 private View mPrefsContainer; 162 163 private boolean mSinglePane; 164 165 // --- State for old mode when showing a single preference list 166 167 private PreferenceManager mPreferenceManager; 168 169 private Bundle mSavedInstanceState; 170 171 // --- Common state 172 173 private Button mNextButton; 174 175 /** 176 * The starting request code given out to preference framework. 177 */ 178 private static final int FIRST_REQUEST_CODE = 100; 179 180 private static final int MSG_BIND_PREFERENCES = 0; 181 private static final int MSG_BUILD_HEADERS = 1; 182 private Handler mHandler = new Handler() { 183 @Override 184 public void handleMessage(Message msg) { 185 switch (msg.what) { 186 case MSG_BIND_PREFERENCES: 187 bindPreferences(); 188 break; 189 case MSG_BUILD_HEADERS: 190 onBuildHeaders(mHeaders); 191 mAdapter.notifyDataSetChanged(); 192 break; 193 } 194 } 195 }; 196 197 private static class HeaderAdapter extends ArrayAdapter<Header> { 198 private static class HeaderViewHolder { 199 ImageView icon; 200 TextView title; 201 TextView summary; 202 } 203 204 private LayoutInflater mInflater; 205 206 public HeaderAdapter(Context context, List<Header> objects) { 207 super(context, 0, objects); 208 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 209 } 210 211 @Override 212 public View getView(int position, View convertView, ViewGroup parent) { 213 HeaderViewHolder holder; 214 View view; 215 216 if (convertView == null) { 217 view = mInflater.inflate(com.android.internal.R.layout.preference_list_item, 218 parent, false); 219 holder = new HeaderViewHolder(); 220 holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon); 221 holder.title = (TextView) view.findViewById(com.android.internal.R.id.title); 222 holder.summary = (TextView) view.findViewById(com.android.internal.R.id.summary); 223 view.setTag(holder); 224 } else { 225 view = convertView; 226 holder = (HeaderViewHolder) view.getTag(); 227 } 228 229 // All view fields must be updated every time, because the view may be recycled 230 Header header = getItem(position); 231 if (header.icon == null) { 232 holder.icon.setImageDrawable(null); 233 holder.icon.setImageResource(header.iconRes); 234 } else { 235 holder.icon.setImageResource(0); 236 holder.icon.setImageDrawable(header.icon); 237 } 238 holder.title.setText(header.title); 239 if (TextUtils.isEmpty(header.summary)) { 240 holder.summary.setVisibility(View.GONE); 241 } else { 242 holder.summary.setVisibility(View.VISIBLE); 243 holder.summary.setText(header.summary); 244 } 245 246 return view; 247 } 248 } 249 250 /** 251 * Description of a single Header item that the user can select. 252 */ 253 public static class Header { 254 /** 255 * Title of the header that is shown to the user. 256 * @attr ref android.R.styleable#PreferenceHeader_title 257 */ 258 public CharSequence title; 259 260 /** 261 * Optional summary describing what this header controls. 262 * @attr ref android.R.styleable#PreferenceHeader_summary 263 */ 264 public CharSequence summary; 265 266 /** 267 * Optional icon resource to show for this header. 268 * @attr ref android.R.styleable#PreferenceHeader_icon 269 */ 270 public int iconRes; 271 272 /** 273 * Optional icon drawable to show for this header. (If this is non-null, 274 * the iconRes will be ignored.) 275 */ 276 public Drawable icon; 277 278 /** 279 * Full class name of the fragment to display when this header is 280 * selected. 281 * @attr ref android.R.styleable#PreferenceHeader_fragment 282 */ 283 public String fragment; 284 285 /** 286 * Optional arguments to supply to the fragment when it is 287 * instantiated. 288 */ 289 public Bundle fragmentArguments; 290 } 291 292 @Override 293 protected void onCreate(Bundle savedInstanceState) { 294 super.onCreate(savedInstanceState); 295 296 setContentView(com.android.internal.R.layout.preference_list_content); 297 298 mPrefsContainer = findViewById(com.android.internal.R.id.prefs); 299 boolean hidingHeaders = onIsHidingHeaders(); 300 mSinglePane = hidingHeaders || !onIsMultiPane(); 301 String initialFragment = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT); 302 Bundle initialArguments = getIntent().getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS); 303 304 if (initialFragment != null && mSinglePane) { 305 // If we are just showing a fragment, we want to run in 306 // new fragment mode, but don't need to compute and show 307 // the headers. 308 getListView().setVisibility(View.GONE); 309 mPrefsContainer.setVisibility(View.VISIBLE); 310 switchToHeader(initialFragment, initialArguments); 311 312 } else { 313 // We need to try to build the headers. 314 onBuildHeaders(mHeaders); 315 316 // If there are headers, then at this point we need to show 317 // them and, depending on the screen, we may also show in-line 318 // the currently selected preference fragment. 319 if (mHeaders.size() > 0) { 320 mAdapter = new HeaderAdapter(this, mHeaders); 321 setListAdapter(mAdapter); 322 if (!mSinglePane) { 323 mPrefsContainer.setVisibility(View.VISIBLE); 324 if (initialFragment == null) { 325 Header h = onGetInitialHeader(); 326 initialFragment = h.fragment; 327 initialArguments = h.fragmentArguments; 328 } 329 switchToHeader(initialFragment, initialArguments); 330 } 331 332 // If there are no headers, we are in the old "just show a screen 333 // of preferences" mode. 334 } else { 335 mPreferenceManager = new PreferenceManager(this, FIRST_REQUEST_CODE); 336 mPreferenceManager.setOnPreferenceTreeClickListener(this); 337 } 338 } 339 340 getListView().setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 341 342 // see if we should show Back/Next buttons 343 Intent intent = getIntent(); 344 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) { 345 346 findViewById(com.android.internal.R.id.button_bar).setVisibility(View.VISIBLE); 347 348 Button backButton = (Button)findViewById(com.android.internal.R.id.back_button); 349 backButton.setOnClickListener(new OnClickListener() { 350 public void onClick(View v) { 351 setResult(RESULT_CANCELED); 352 finish(); 353 } 354 }); 355 Button skipButton = (Button)findViewById(com.android.internal.R.id.skip_button); 356 skipButton.setOnClickListener(new OnClickListener() { 357 public void onClick(View v) { 358 setResult(RESULT_OK); 359 finish(); 360 } 361 }); 362 mNextButton = (Button)findViewById(com.android.internal.R.id.next_button); 363 mNextButton.setOnClickListener(new OnClickListener() { 364 public void onClick(View v) { 365 setResult(RESULT_OK); 366 finish(); 367 } 368 }); 369 370 // set our various button parameters 371 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) { 372 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT); 373 if (TextUtils.isEmpty(buttonText)) { 374 mNextButton.setVisibility(View.GONE); 375 } 376 else { 377 mNextButton.setText(buttonText); 378 } 379 } 380 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) { 381 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT); 382 if (TextUtils.isEmpty(buttonText)) { 383 backButton.setVisibility(View.GONE); 384 } 385 else { 386 backButton.setText(buttonText); 387 } 388 } 389 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) { 390 skipButton.setVisibility(View.VISIBLE); 391 } 392 } 393 } 394 395 /** 396 * Returns true if this activity is currently showing the header list. 397 */ 398 public boolean hasHeaders() { 399 return getListView().getVisibility() == View.VISIBLE 400 && mPreferenceManager == null; 401 } 402 403 /** 404 * Returns true if this activity is showing multiple panes -- the headers 405 * and a preference fragment. 406 */ 407 public boolean isMultiPane() { 408 return hasHeaders() && mPrefsContainer.getVisibility() == View.VISIBLE; 409 } 410 411 /** 412 * Called to determine if the activity should run in multi-pane mode. 413 * The default implementation returns true if the screen is large 414 * enough. 415 */ 416 public boolean onIsMultiPane() { 417 Configuration config = getResources().getConfiguration(); 418 if ((config.screenLayout&Configuration.SCREENLAYOUT_SIZE_MASK) 419 == Configuration.SCREENLAYOUT_SIZE_XLARGE 420 && config.orientation == Configuration.ORIENTATION_LANDSCAPE) { 421 return true; 422 } 423 return false; 424 } 425 426 /** 427 * Called to determine whether the header list should be hidden. 428 * The default implementation returns the 429 * value given in {@link #EXTRA_NO_HEADERS} or false if it is not supplied. 430 * This is set to false, for example, when the activity is being re-launched 431 * to show a particular preference activity. 432 */ 433 public boolean onIsHidingHeaders() { 434 return getIntent().getBooleanExtra(EXTRA_NO_HEADERS, false); 435 } 436 437 /** 438 * Called to determine the initial header to be shown. The default 439 * implementation simply returns the fragment of the first header. Note 440 * that the returned Header object does not actually need to exist in 441 * your header list -- whatever its fragment is will simply be used to 442 * show for the initial UI. 443 */ 444 public Header onGetInitialHeader() { 445 return mHeaders.get(0); 446 } 447 448 /** 449 * Called when the activity needs its list of headers build. By 450 * implementing this and adding at least one item to the list, you 451 * will cause the activity to run in its modern fragment mode. Note 452 * that this function may not always be called; for example, if the 453 * activity has been asked to display a particular fragment without 454 * the header list, there is no need to build the headers. 455 * 456 * <p>Typical implementations will use {@link #loadHeadersFromResource} 457 * to fill in the list from a resource. 458 * 459 * @param target The list in which to place the headers. 460 */ 461 public void onBuildHeaders(List<Header> target) { 462 } 463 464 /** 465 * Call when you need to change the headers being displayed. Will result 466 * in onBuildHeaders() later being called to retrieve the new list. 467 */ 468 public void invalidateHeaders() { 469 if (!mHandler.hasMessages(MSG_BUILD_HEADERS)) { 470 mHandler.sendEmptyMessage(MSG_BUILD_HEADERS); 471 } 472 } 473 474 /** 475 * Parse the given XML file as a header description, adding each 476 * parsed Header into the target list. 477 * 478 * @param resid The XML resource to load and parse. 479 * @param target The list in which the parsed headers should be placed. 480 */ 481 public void loadHeadersFromResource(int resid, List<Header> target) { 482 XmlResourceParser parser = null; 483 try { 484 parser = getResources().getXml(resid); 485 AttributeSet attrs = Xml.asAttributeSet(parser); 486 487 int type; 488 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT 489 && type != XmlPullParser.START_TAG) { 490 } 491 492 String nodeName = parser.getName(); 493 if (!"preference-headers".equals(nodeName)) { 494 throw new RuntimeException( 495 "XML document must start with <preference-headers> tag; found" 496 + nodeName + " at " + parser.getPositionDescription()); 497 } 498 499 Bundle curBundle = null; 500 501 int outerDepth = parser.getDepth(); 502 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT 503 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 504 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 505 continue; 506 } 507 508 nodeName = parser.getName(); 509 if ("header".equals(nodeName)) { 510 Header header = new Header(); 511 512 TypedArray sa = getResources().obtainAttributes(attrs, 513 com.android.internal.R.styleable.PreferenceHeader); 514 header.title = sa.getText( 515 com.android.internal.R.styleable.PreferenceHeader_title); 516 header.summary = sa.getText( 517 com.android.internal.R.styleable.PreferenceHeader_summary); 518 header.iconRes = sa.getResourceId( 519 com.android.internal.R.styleable.PreferenceHeader_icon, 0); 520 header.fragment = sa.getString( 521 com.android.internal.R.styleable.PreferenceHeader_fragment); 522 sa.recycle(); 523 524 if (curBundle == null) { 525 curBundle = new Bundle(); 526 } 527 getResources().parseBundleExtras(parser, curBundle); 528 if (curBundle.size() > 0) { 529 header.fragmentArguments = curBundle; 530 curBundle = null; 531 } 532 533 target.add(header); 534 } else { 535 XmlUtils.skipCurrentTag(parser); 536 } 537 } 538 539 } catch (XmlPullParserException e) { 540 throw new RuntimeException("Error parsing headers", e); 541 } catch (IOException e) { 542 throw new RuntimeException("Error parsing headers", e); 543 } finally { 544 if (parser != null) parser.close(); 545 } 546 547 } 548 549 @Override 550 protected void onStop() { 551 super.onStop(); 552 553 if (mPreferenceManager != null) { 554 mPreferenceManager.dispatchActivityStop(); 555 } 556 } 557 558 @Override 559 protected void onDestroy() { 560 super.onDestroy(); 561 562 if (mPreferenceManager != null) { 563 mPreferenceManager.dispatchActivityDestroy(); 564 } 565 } 566 567 @Override 568 protected void onSaveInstanceState(Bundle outState) { 569 super.onSaveInstanceState(outState); 570 571 if (mPreferenceManager != null) { 572 final PreferenceScreen preferenceScreen = getPreferenceScreen(); 573 if (preferenceScreen != null) { 574 Bundle container = new Bundle(); 575 preferenceScreen.saveHierarchyState(container); 576 outState.putBundle(PREFERENCES_TAG, container); 577 } 578 } 579 } 580 581 @Override 582 protected void onRestoreInstanceState(Bundle state) { 583 if (mPreferenceManager != null) { 584 Bundle container = state.getBundle(PREFERENCES_TAG); 585 if (container != null) { 586 final PreferenceScreen preferenceScreen = getPreferenceScreen(); 587 if (preferenceScreen != null) { 588 preferenceScreen.restoreHierarchyState(container); 589 mSavedInstanceState = state; 590 return; 591 } 592 } 593 } 594 595 // Only call this if we didn't save the instance state for later. 596 // If we did save it, it will be restored when we bind the adapter. 597 super.onRestoreInstanceState(state); 598 } 599 600 @Override 601 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 602 super.onActivityResult(requestCode, resultCode, data); 603 604 if (mPreferenceManager != null) { 605 mPreferenceManager.dispatchActivityResult(requestCode, resultCode, data); 606 } 607 } 608 609 @Override 610 public void onContentChanged() { 611 super.onContentChanged(); 612 613 if (mPreferenceManager != null) { 614 postBindPreferences(); 615 } 616 } 617 618 @Override 619 protected void onListItemClick(ListView l, View v, int position, long id) { 620 super.onListItemClick(l, v, position, id); 621 622 if (mAdapter != null) { 623 onHeaderClick(mHeaders.get(position), position); 624 } 625 } 626 627 /** 628 * Called when the user selects an item in the header list. The default 629 * implementation will call either {@link #startWithFragment(String, Bundle)} 630 * or {@link #switchToHeader(String, Bundle)} as appropriate. 631 * 632 * @param header The header that was selected. 633 * @param position The header's position in the list. 634 */ 635 public void onHeaderClick(Header header, int position) { 636 if (mSinglePane) { 637 startWithFragment(header.fragment, header.fragmentArguments); 638 } else { 639 switchToHeader(header.fragment, header.fragmentArguments); 640 } 641 } 642 643 /** 644 * Start a new instance of this activity, showing only the given 645 * preference fragment. When launched in this mode, the header list 646 * will be hidden and the given preference fragment will be instantiated 647 * and fill the entire activity. 648 * 649 * @param fragmentName The name of the fragment to display. 650 * @param args Optional arguments to supply to the fragment. 651 */ 652 public void startWithFragment(String fragmentName, Bundle args) { 653 Intent intent = new Intent(Intent.ACTION_MAIN); 654 intent.setClass(this, getClass()); 655 intent.putExtra(EXTRA_SHOW_FRAGMENT, fragmentName); 656 intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args); 657 intent.putExtra(EXTRA_NO_HEADERS, true); 658 startActivity(intent); 659 } 660 661 /** 662 * When in two-pane mode, switch the fragment pane to show the given 663 * preference fragment. 664 * 665 * @param fragmentName The name of the fragment to display. 666 * @param args Optional arguments to supply to the fragment. 667 */ 668 public void switchToHeader(String fragmentName, Bundle args) { 669 getFragmentManager().popBackStack(BACK_STACK_PREFS, POP_BACK_STACK_INCLUSIVE); 670 671 Fragment f = Fragment.instantiate(this, fragmentName, args); 672 getFragmentManager().openTransaction().replace( 673 com.android.internal.R.id.prefs, f).commit(); 674 } 675 676 @Override 677 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) { 678 Fragment f = Fragment.instantiate(this, pref.getFragment(), pref.getExtras()); 679 getFragmentManager().openTransaction().replace(com.android.internal.R.id.prefs, f) 680 .addToBackStack(BACK_STACK_PREFS).commit(); 681 return true; 682 } 683 684 /** 685 * Posts a message to bind the preferences to the list view. 686 * <p> 687 * Binding late is preferred as any custom preference types created in 688 * {@link #onCreate(Bundle)} are able to have their views recycled. 689 */ 690 private void postBindPreferences() { 691 if (mHandler.hasMessages(MSG_BIND_PREFERENCES)) return; 692 mHandler.obtainMessage(MSG_BIND_PREFERENCES).sendToTarget(); 693 } 694 695 private void bindPreferences() { 696 final PreferenceScreen preferenceScreen = getPreferenceScreen(); 697 if (preferenceScreen != null) { 698 preferenceScreen.bind(getListView()); 699 if (mSavedInstanceState != null) { 700 super.onRestoreInstanceState(mSavedInstanceState); 701 mSavedInstanceState = null; 702 } 703 } 704 } 705 706 /** 707 * Returns the {@link PreferenceManager} used by this activity. 708 * @return The {@link PreferenceManager}. 709 * 710 * @deprecated This function is not relevant for a modern fragment-based 711 * PreferenceActivity. 712 */ 713 @Deprecated 714 public PreferenceManager getPreferenceManager() { 715 return mPreferenceManager; 716 } 717 718 private void requirePreferenceManager() { 719 if (mPreferenceManager == null) { 720 if (mAdapter == null) { 721 throw new RuntimeException("This should be called after super.onCreate."); 722 } 723 throw new RuntimeException( 724 "Modern two-pane PreferenceActivity requires use of a PreferenceFragment"); 725 } 726 } 727 728 /** 729 * Sets the root of the preference hierarchy that this activity is showing. 730 * 731 * @param preferenceScreen The root {@link PreferenceScreen} of the preference hierarchy. 732 * 733 * @deprecated This function is not relevant for a modern fragment-based 734 * PreferenceActivity. 735 */ 736 @Deprecated 737 public void setPreferenceScreen(PreferenceScreen preferenceScreen) { 738 requirePreferenceManager(); 739 740 if (mPreferenceManager.setPreferences(preferenceScreen) && preferenceScreen != null) { 741 postBindPreferences(); 742 CharSequence title = getPreferenceScreen().getTitle(); 743 // Set the title of the activity 744 if (title != null) { 745 setTitle(title); 746 } 747 } 748 } 749 750 /** 751 * Gets the root of the preference hierarchy that this activity is showing. 752 * 753 * @return The {@link PreferenceScreen} that is the root of the preference 754 * hierarchy. 755 * 756 * @deprecated This function is not relevant for a modern fragment-based 757 * PreferenceActivity. 758 */ 759 @Deprecated 760 public PreferenceScreen getPreferenceScreen() { 761 if (mPreferenceManager != null) { 762 return mPreferenceManager.getPreferenceScreen(); 763 } 764 return null; 765 } 766 767 /** 768 * Adds preferences from activities that match the given {@link Intent}. 769 * 770 * @param intent The {@link Intent} to query activities. 771 * 772 * @deprecated This function is not relevant for a modern fragment-based 773 * PreferenceActivity. 774 */ 775 @Deprecated 776 public void addPreferencesFromIntent(Intent intent) { 777 requirePreferenceManager(); 778 779 setPreferenceScreen(mPreferenceManager.inflateFromIntent(intent, getPreferenceScreen())); 780 } 781 782 /** 783 * Inflates the given XML resource and adds the preference hierarchy to the current 784 * preference hierarchy. 785 * 786 * @param preferencesResId The XML resource ID to inflate. 787 * 788 * @deprecated This function is not relevant for a modern fragment-based 789 * PreferenceActivity. 790 */ 791 @Deprecated 792 public void addPreferencesFromResource(int preferencesResId) { 793 requirePreferenceManager(); 794 795 setPreferenceScreen(mPreferenceManager.inflateFromResource(this, preferencesResId, 796 getPreferenceScreen())); 797 } 798 799 /** 800 * {@inheritDoc} 801 * 802 * @deprecated This function is not relevant for a modern fragment-based 803 * PreferenceActivity. 804 */ 805 @Deprecated 806 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { 807 return false; 808 } 809 810 /** 811 * Finds a {@link Preference} based on its key. 812 * 813 * @param key The key of the preference to retrieve. 814 * @return The {@link Preference} with the key, or null. 815 * @see PreferenceGroup#findPreference(CharSequence) 816 * 817 * @deprecated This function is not relevant for a modern fragment-based 818 * PreferenceActivity. 819 */ 820 @Deprecated 821 public Preference findPreference(CharSequence key) { 822 823 if (mPreferenceManager == null) { 824 return null; 825 } 826 827 return mPreferenceManager.findPreference(key); 828 } 829 830 @Override 831 protected void onNewIntent(Intent intent) { 832 if (mPreferenceManager != null) { 833 mPreferenceManager.dispatchNewIntent(intent); 834 } 835 } 836 837 // give subclasses access to the Next button 838 /** @hide */ 839 protected boolean hasNextButton() { 840 return mNextButton != null; 841 } 842 /** @hide */ 843 protected Button getNextButton() { 844 return mNextButton; 845 } 846} 847