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

前端 未结 4 723
小鲜肉
小鲜肉 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 18:16

    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 :

    
    
    
         
    
            
    
    
    

    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

提交回复
热议问题