Non Deprecated findPreference() Method? - Android

前端 未结 4 1398
梦毁少年i
梦毁少年i 2020-12-02 20:22

I want to detect when a Preference contained in a ListView gets clicked, so that I can launch an intent to manage that selection.

I would h

4条回答
  •  有刺的猬
    2020-12-02 20:28

    Is there a non deprecated equivalent?

    If you are using PreferenceFragment on API Level 11+ devices, you would call findPreference() on it. Otherwise, call findPreference() on your PreferenceActivity, as you have no choice.

    If not, what if I use it anyway?

    It will work.

    How can Fragments help me do my task in a better way?

    API Level 11+ introduced PreferenceFragment as another way of constructing the contents of a PreferenceActivity. You are welcome to use them, but if you are still supporting older devices, you cannot use PreferenceFragment for those devices.

    That being said:

    I want to detect when a Preference contained in a ListView gets clicked, so that I can launch an intent to manage that selection.

    You do not need Java code for this. Use:

        
    
            
    
        
    

    (as seen in the JavaDocs for PreferenceActivity)

    This will create an entry in the preference UI that, when clicked, will start an activity with the specified Intent.

提交回复
热议问题