I have write a multi-level Preferences in program with xml File. and i want to know how to return to the previous level without press the back button. how to write some code to implement an item in preference to return previous Preferences level.
4 Answers
I had the same problem and solve it thanks to Xuelong and getDialog() but without needing to manage onPreferenceTreeClick().
- You need to keep an instance (myPreferenceScreen) of the PreferenceScreen you want to return from
- You have to give him a key in XML
- Retrieve the instance with findPreference("MyPreferenceScreenKey");
- Once you have to return , use this method : myPreferenceScreen.getDialog().dismiss()
You will then return from where you came from.
Here is a epurated example :
Xml file :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference android:key="contactList"/>
<PreferenceScreen android:title="My Sub Preference Screen ..."
android:key="mySubScreenKey">
<EditTextPreference android:key="foo1"/>
</PreferenceScreen>
</PreferenceScreen>
Java file :
public class ParanoidPreferenceManager extends PreferenceActivity {
ListPreference contactList;
EditTextPreference foo1;
PreferenceScreen mySubScreenKey;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Load XML preference file
addPreferencesFromResource(R.xml.preferences);
contactList = (ListPreference) findPreference("contactList");
foo1= (EditTextPreference) findPreference("foo1");
screenContact = (PreferenceScreen) findPreference("screenAddContact");
foo1.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
mySubScreenKey.getDialog().dismiss();
return false;
}
});
}
}
That's it
Sorry for presentation, this is my first post on this site.
Bye dudes

- 21
- 1
-
Yeah! Good job. Thanks to share. this is a good way in a preferenceActivity which has a little preference item and less level. In my application, I've managed a large PreferenceActivity and it has many return Preference, then write code in onPreferenceTreeClick() is a good way to make every return item work without write different Listener for every Preferences. Ha and this Question is the first one i post here too. lol~~ – Xuelong Sep 02 '11 at 06:18
Building on Patrice's and Xuelong's answers, I have made the following functions to easily do the job:
// Refresh the content of a PreferenceScreen by simulating a click (thus it will call the refresh code if it's contained inside the click callback)
private void refreshPreferenceScreenByClick(String prefScreenName) {
// Refresh preference screen (given by its name) by calling the click callback
PreferenceScreen prefScreen = (PreferenceScreen) findPreference(prefScreenName);
Preference.OnPreferenceClickListener click_callback = prefScreen.getOnPreferenceClickListener();
click_callback.onPreferenceClick(prefScreen);
}
// Close the current PreferenceScreen (or any given the name)
// Useful to go back to the previous PreferenceScreen when constructing dynamically nested submenus.
private void closePreferenceScreen(String prefScreenName) {
PreferenceScreen prefScreen = (PreferenceScreen) findPreference(prefScreenName);
prefScreen.getDialog().dismiss();
}
So first if what you do in your submenu define what is shown on the parent menu (eg in a music app: submenu allows to add a new instrument, and the parent menu shows the list of added instruments), you first have to call refreshPreferenceScreenByClick("parentPreferenceScreen")
, and then to go back to the parent menu you can just close the current submenu by calling closePreferenceScreen("currentPreferenceScreen")
.
Alternatively, if you want to just open a submenu programmatically, you can use the openPreference function found here (tested and it works well): https://stackoverflow.com/a/4869034/1121352
NOTE: do NOT use the openPreference() function to go back to parent PreferenceScreen, because after a few iterations you will get a stack overflow and your app will crash (because Android will keep in memory the history of all previous menus, including the ones you open via openPreference()!).
You can simulate the press of the back button:
this.onBackPressed();
or
getActivity().onBackPressed();
if you are in a fragment.

- 623
- 10
- 17
I think you are looking for something like this
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="NotifSettings" android:title="Notification Settings">
<PreferenceCategory android:title="Notification Settings">
<CheckBoxPreference android:key="NotifyOption"
android:title="Notification Status" android:defaultValue="true"
android:summaryOn="Notifications are Enabled." android:summaryOff="Notifications are Disabled."
android:persistent="true" />
<Preference android:title="XXXX"
android:dependency="NotifyOption" android:summary="xxxxxx"
android:key="Xxx" />
</PreferenceCategory>
<PreferenceCategory android:title="YYYYY">
<PreferenceScreen android:title="Configure Notifications"
android:summary="yyyyy">
<CheckBoxPreference android:key="aa"
android:title="aaaaa" android:defaultValue="true"
android:summary="aaaa" />
<CheckBoxPreference android:key="bb"
android:title="bbbbb" android:defaultValue="true"
android:summary="bbbb." />
...
<ListPreference android:title="zzz"
android:summary="zzzz" android:key="zz"
android:defaultValue="0" android:entries="@array/OverDesc"
android:entryValues="@array/OverValues" />
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title="Alert Type">
<CheckBoxPreference android:key="AlertVibr"
android:title="Vibrate" android:defaultValue="true"
android:summaryOn="Vibration Turned ON" android:summaryOff="Vibration Turned OFF" />
<CheckBoxPreference android:key="AlertSound"
android:title="Sound" android:defaultValue="true" android:summaryOn="Sound Turned ON"
android:summaryOff="MUTE" />
</PreferenceCategory>
But what I didnt get is that your want to return to the previous preference screen, without pressing back button. Can you shed some more light on that??

- 1,807
- 3
- 22
- 48
-
ah...I write the PreferencesActiviry in multi-level, for example click on the first item in the Preferences list,it will turn in to another new Preference menu..just like file folder in OS. and then, how to return to the parent Preference menu just like to return to the parent folder in OS. I'm sorry for my pool English.. – Xuelong Aug 09 '11 at 00:39
-
I found the solution, just write code to getDialog in onPreferenceTreeClick method, then call dialog's dismiss method then it will return(for example in your xml code, in Configure Notifications menu turn to the main preferenceScreen). – Xuelong Aug 09 '11 at 09:07
-
oh Thank you. I meet an new problem, i post [link](http://stackoverflow.com/q/6995154/883898) here. – Xuelong Aug 09 '11 at 11:06