Skip to content

Instantly share code, notes, and snippets.

@JakeWharton
Created July 26, 2011 23:39
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JakeWharton/1108374 to your computer and use it in GitHub Desktop.
Save JakeWharton/1108374 to your computer and use it in GitHub Desktop.
Beginning of a PreferenceFragment implementation that will work back to Android 1.6
package android.support.v4.preference;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public abstract class PreferenceFragment extends Fragment {
private static final int FIRST_REQUEST_CODE = 100;
private static final int MSG_BIND_PREFERENCES = 1;
private static final String PREFERENCES_TAG = "android:preferences";
private Handler mHandler;
private boolean mHavePrefs;
private boolean mInitDone;
private ListView mList;
private PreferenceManager mPreferenceManager;
private final Runnable mRequestFocus;
public PreferenceFragment() {
// PreferenceFragment.1 local1 = new PreferenceFragment.1(this);
// this.mHandler = local1;
// PreferenceFragment.2 local2 = new PreferenceFragment.2(this);
// this.mRequestFocus = local2;
mRequestFocus = null;
}
private void bindPreferences() {
PreferenceScreen localPreferenceScreen = getPreferenceScreen();
if (localPreferenceScreen != null) {
ListView localListView = getListView();
localPreferenceScreen.bind(localListView);
}
}
private void ensureList() {
if (mList == null) {
View view = getView();
if (view == null) {
throw new IllegalStateException("Content view not yet created");
}
View listView = view.findViewById(android.R.id.list);
if (!(listView instanceof ListView)) {
throw new RuntimeException("Content has view with id attribute 'android.R.id.list' that is not a ListView class");
}
mList = (ListView)listView;
if (mList == null) {
throw new RuntimeException("Your content must have a ListView whose id attribute is 'android.R.id.list'");
}
mHandler.post(mRequestFocus);
}
}
private void postBindPreferences() {
if (mHandler.hasMessages(MSG_BIND_PREFERENCES)) {
mHandler.obtainMessage(MSG_BIND_PREFERENCES).sendToTarget();
}
}
private void requirePreferenceManager() {
if (this.mPreferenceManager == null) {
throw new RuntimeException("This should be called after super.onCreate.");
}
}
public void addPreferencesFromIntent(Intent intent) {
requirePreferenceManager();
PreferenceScreen screen = mPreferenceManager.inflateFromIntent(intent, getPreferenceScreen());
setPreferenceScreen(screen);
}
public void addPreferencesFromResource(int resId) {
requirePreferenceManager();
PreferenceScreen screen = mPreferenceManager.inflateFromResource(getActivity(), resId, getPreferenceScreen());
setPreferenceScreen(screen);
}
public Preference findPreference(CharSequence key) {
if (mPreferenceManager == null) {
return null;
}
return mPreferenceManager.findPreference(key);
}
public ListView getListView() {
ensureList();
return mList;
}
public PreferenceManager getPreferenceManager() {
return mPreferenceManager;
}
public PreferenceScreen getPreferenceScreen() {
return mPreferenceManager.getPreferenceScreen();
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setScrollBarStyle(0);
if (mHavePrefs) {
bindPreferences();
}
mInitDone = true;
if (savedInstanceState != null) {
Bundle localBundle = savedInstanceState.getBundle(PREFERENCES_TAG);
if (localBundle != null) {
PreferenceScreen screen = getPreferenceScreen();
if (screen != null) {
screen.restoreHierarchyState(localBundle);
}
}
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mPreferenceManager.dispatchActivityResult(requestCode, resultCode, data);
}
public void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
mPreferenceManager = new PreferenceManager(getActivity(), FIRST_REQUEST_CODE);
mPreferenceManager.setFragment(this);
mPreferenceManager.setOnPreferenceTreeClickListener(this);
}
public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle) {
return paramLayoutInflater.inflate(17367145, paramViewGroup, 0);
}
public void onDestroy() {
super.onDestroy();
mPreferenceManager.dispatchActivityDestroy();
mPreferenceManager.setOnPreferenceTreeClickListener(null);
}
public void onDestroyView() {
mList = null;
mHandler.removeCallbacks(mRequestFocus);
mHandler.removeMessages(MSG_BIND_PREFERENCES);
super.onDestroyView();
}
public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
if ((preference.getFragment() != null) && ((getActivity() instanceof OnPreferenceStartFragmentCallback))) {
return ((OnPreferenceStartFragmentCallback)getActivity()).onPreferenceStartFragment(this, preference);
}
return false;
}
public void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
PreferenceScreen screen = getPreferenceScreen();
if (screen != null) {
Bundle localBundle = new Bundle();
screen.saveHierarchyState(localBundle);
bundle.putBundle(PREFERENCES_TAG, localBundle);
}
}
public void onStop() {
super.onStop();
mPreferenceManager.dispatchActivityStop();
}
public void setPreferenceScreen(PreferenceScreen screen) {
if ((mPreferenceManager.setPreferences(screen)) && (screen != null)) {
mHavePrefs = true;
if (mInitDone) {
postBindPreferences();
}
}
}
public abstract interface OnPreferenceStartFragmentCallback {
public abstract boolean onPreferenceStartFragment(PreferenceFragment paramPreferenceFragment, Preference paramPreference);
}
}
@sermojohn
Copy link

Hello!

Could you please help me with this?

How could I put this class in use? Tried to copy it in my own project, but there are some calls to PreferenceManager methods, which do not exist the Android API! What I got is that I should include recompile the support.v4 library and include this class, right?

Thanks in advance!

@Starksoft
Copy link

This is the implementation of actionbarsherlock, you dont need to include this file in your project, just wait for stable release.
Jake, thank you for your work.

@xalexchen
Copy link

when will pack into actionbarsherlock ? i think i need it now .

@EatHeat
Copy link

EatHeat commented Dec 9, 2013

Any update on this? Really need it. :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment