@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (mPreferenceManager != null) {
      mPreferenceManager.dispatchActivityResult(requestCode, resultCode, data);
    }
  }
  @Override
  public void onContentChanged() {
    super.onContentChanged();

    if (mPreferenceManager != null) {
      postBindPreferences();
    }
  }
  @Override
  protected void onDestroy() {
    super.onDestroy();

    if (mPreferenceManager != null) {
      mPreferenceManager.dispatchActivityDestroy();
    }
  }
  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    if (mAdapter != null) {
      Object item = mAdapter.getItem(position);
      if (item instanceof Header) onHeaderClick((Header) item, position);
    }
  }
 private void bindPreferences() {
   final PreferenceScreen preferenceScreen = getPreferenceScreen();
   if (preferenceScreen != null) {
     preferenceScreen.bind(getListView());
     if (mSavedInstanceState != null) {
       super.onRestoreInstanceState(mSavedInstanceState);
       mSavedInstanceState = null;
     }
   }
 }
  @Override
  protected void onRestoreInstanceState(Bundle state) {
    if (mPreferenceManager != null) {
      Bundle container = state.getBundle(PREFERENCES_TAG);
      if (container != null) {
        final PreferenceScreen preferenceScreen = getPreferenceScreen();
        if (preferenceScreen != null) {
          preferenceScreen.restoreHierarchyState(container);
          mSavedInstanceState = state;
          return;
        }
      }
    }

    // Only call this if we didn't save the instance state for later.
    // If we did save it, it will be restored when we bind the adapter.
    super.onRestoreInstanceState(state);
  }
  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    if (mHeaders.size() > 0) {
      outState.putParcelableArrayList(HEADERS_TAG, mHeaders);
      if (mCurHeader != null) {
        int index = mHeaders.indexOf(mCurHeader);
        if (index >= 0) {
          outState.putInt(CUR_HEADER_TAG, index);
        }
      }
    }

    if (mPreferenceManager != null) {
      final PreferenceScreen preferenceScreen = getPreferenceScreen();
      if (preferenceScreen != null) {
        Bundle container = new Bundle();
        preferenceScreen.saveHierarchyState(container);
        outState.putBundle(PREFERENCES_TAG, container);
      }
    }
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.preference_list_content);

    mListFooter = (FrameLayout) findViewById(R.id.list_footer);
    mPrefsContainer = (ViewGroup) findViewById(R.id.prefs_frame);
    boolean hidingHeaders = onIsHidingHeaders();
    mSinglePane = hidingHeaders || !onIsMultiPane();
    String initialFragment = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
    Bundle initialArguments = getIntent().getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
    int initialTitle = getIntent().getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE, 0);
    int initialShortTitle = getIntent().getIntExtra(EXTRA_SHOW_FRAGMENT_SHORT_TITLE, 0);

    if (savedInstanceState != null) {
      // We are restarting from a previous saved state; used that to
      // initialize, instead of starting fresh.
      ArrayList<Header> headers = savedInstanceState.getParcelableArrayList(HEADERS_TAG);
      if (headers != null) {
        mHeaders.addAll(headers);
        int curHeader = savedInstanceState.getInt(CUR_HEADER_TAG, (int) HEADER_ID_UNDEFINED);
        if (curHeader >= 0 && curHeader < mHeaders.size()) {
          setSelectedHeader(mHeaders.get(curHeader));
        }
      }

    } else {
      if (initialFragment != null && mSinglePane) {
        // If we are just showing a fragment, we want to run in
        // new fragment mode, but don't need to compute and show
        // the headers.
        switchToHeader(initialFragment, initialArguments);
        if (initialTitle != 0) {
          CharSequence initialTitleStr = getText(initialTitle);
          CharSequence initialShortTitleStr =
              initialShortTitle != 0 ? getText(initialShortTitle) : null;
          showBreadCrumbs(initialTitleStr, initialShortTitleStr);
        }

      } else {
        // We need to try to build the headers.
        onBuildHeaders(mHeaders);

        // If there are headers, then at this point we need to show
        // them and, depending on the screen, we may also show in-line
        // the currently selected preference fragment.
        if (mHeaders.size() > 0) {
          if (!mSinglePane) {
            if (initialFragment == null) {
              Header h = onGetInitialHeader();
              switchToHeader(h);
            } else {
              switchToHeader(initialFragment, initialArguments);
            }
          }
        }
      }
    }

    // The default configuration is to only show the list view.  Adjust
    // visibility for other configurations.
    if (initialFragment != null && mSinglePane) {
      // Single pane, showing just a prefs fragment.
      findViewById(R.id.headers).setVisibility(View.GONE);
      mPrefsContainer.setVisibility(View.VISIBLE);
      if (initialTitle != 0) {
        CharSequence initialTitleStr = getText(initialTitle);
        CharSequence initialShortTitleStr =
            initialShortTitle != 0 ? getText(initialShortTitle) : null;
        showBreadCrumbs(initialTitleStr, initialShortTitleStr);
      }
    } else if (mHeaders.size() > 0) {
      setListAdapter(new HeaderAdapter(this, mHeaders));
      if (!mSinglePane) {
        // Multi-pane.
        getListView().setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        if (mCurHeader != null) {
          setSelectedHeader(mCurHeader);
        }
        mPrefsContainer.setVisibility(View.VISIBLE);
      }
    } else {
      // If there are no headers, we are in the old "just show a screen
      // of preferences" mode.
      setContentView(R.layout.preference_list_content_single);
      mListFooter = (FrameLayout) findViewById(R.id.list_footer);
      mPrefsContainer = (ViewGroup) findViewById(R.id.prefs);
      mPreferenceManager = new PreferenceManager(this, FIRST_REQUEST_CODE);
      mPreferenceManager.setOnPreferenceTreeClickListener(this);
    }

    // see if we should show Back/Next buttons
    Intent intent = getIntent();
    if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {

      findViewById(R.id.button_bar).setVisibility(View.VISIBLE);

      Button backButton = (Button) findViewById(R.id.back_button);
      backButton.setOnClickListener(
          new OnClickListener() {
            public void onClick(View v) {
              setResult(RESULT_CANCELED);
              finish();
            }
          });
      Button skipButton = (Button) findViewById(R.id.skip_button);
      skipButton.setOnClickListener(
          new OnClickListener() {
            public void onClick(View v) {
              setResult(RESULT_OK);
              finish();
            }
          });
      mNextButton = (Button) findViewById(R.id.next_button);
      mNextButton.setOnClickListener(
          new OnClickListener() {
            public void onClick(View v) {
              setResult(RESULT_OK);
              finish();
            }
          });

      // set our various button parameters
      if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
        String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
        if (TextUtils.isEmpty(buttonText)) {
          mNextButton.setVisibility(View.GONE);
        } else {
          mNextButton.setText(buttonText);
        }
      }
      if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
        String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
        if (TextUtils.isEmpty(buttonText)) {
          backButton.setVisibility(View.GONE);
        } else {
          backButton.setText(buttonText);
        }
      }
      if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
        skipButton.setVisibility(View.VISIBLE);
      }
    }
  }