how to return to the previous PrefenenceScreen from nested PreferenceScreen without press the back button

前端 未结 4 701
小鲜肉
小鲜肉 2021-01-06 17:19

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

相关标签:
4条回答
  • 2021-01-06 17:56

    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??

    0 讨论(0)
  • 2021-01-06 18:10

    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()!).

    0 讨论(0)
  • I had the same problem and solve it thanks to Xuelong and getDialog() but without needing to manage onPreferenceTreeClick().

    1. You need to keep an instance (myPreferenceScreen) of the PreferenceScreen you want to return from
    2. You have to give him a key in XML
    3. Retrieve the instance with findPreference("MyPreferenceScreenKey");
    4. 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

    0 讨论(0)
  • 2021-01-06 18:22

    You can simulate the press of the back button:

    this.onBackPressed();
    

    or

    getActivity().onBackPressed();
    

    if you are in a fragment.

    0 讨论(0)
提交回复
热议问题