You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

findPreference方法已废弃,应调用何种替代方法?附示例代码

Replacing Deprecated findPreference() in Android

Hey there! Since the old findPreference() method from PreferenceActivity got deprecated, the modern replacement relies on using the AndroidX Preference Library (the standard for all new Android apps) and working with PreferenceFragmentCompat—here's how to do it properly:

The current best practice is to manage your app's settings within a PreferenceFragmentCompat subclass. This class provides a non-deprecated findPreference() method that works exactly like the old one, but fits better with modern Android architecture.

Kotlin Example

import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import android.os.Bundle

class SettingsFragment : PreferenceFragmentCompat() {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        // Load your preferences XML file
        setPreferencesFromResource(R.xml.app_preferences, rootKey)

        // Get a reference to your preference using its key
        val notificationPreference: Preference? = findPreference("key_notifications")

        // Use the preference (e.g., set a click listener)
        notificationPreference?.setOnPreferenceClickListener {
            // Handle the click event here
            true
        }
    }
}

Java Example

import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import android.os.Bundle;

public class SettingsFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.app_preferences, rootKey);

        // Retrieve the preference by its key
        Preference notificationPreference = findPreference("key_notifications");
        
        if (notificationPreference != null) {
            notificationPreference.setOnPreferenceClickListener(preference -> {
                // Handle click logic
                return true;
            });
        }
    }
}

Accessing Preferences from an Activity (If Necessary)

If you need to access a preference from your host Activity (not recommended unless absolutely required), first get a reference to your PreferenceFragmentCompat instance, then call its findPreference() method:

Kotlin Example

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class SettingsActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)

        // Get the fragment from your layout or fragment manager
        val settingsFragment = supportFragmentManager
            .findFragmentById(R.id.fragment_settings) as? SettingsFragment

        // Access the preference via the fragment
        val themePreference = settingsFragment?.findPreference<Preference>("key_app_theme")
    }
}

Important Setup Note

Make sure your project includes the AndroidX Preference Library dependency in your module-level build.gradle file:

dependencies {
    implementation "androidx.preference:preference-ktx:1.2.0" // Use the latest version available
}

This approach keeps your code aligned with Android's current best practices, handles configuration changes smoothly, and avoids deprecated APIs.

内容的提问来源于stack exchange,提问作者Aditya Jha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:08:24