android - NotificationPreferenceFragment - 默认构造函数已弃用

标签 android android-fragments android-preferences

我的代码是:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class NotificationPreferenceFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_notification);
        setHasOptionsMenu(true);
        bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
        Preference notificationPref = findPreference("notifications_new_message");
        notificationPref.setOnPreferenceChangeListener((preference, newValue) -> {
            boolean isNotificationOn = (Boolean) newValue;
            if(preference.getKey().equals("notifications_new_message") && isNotificationOn){
                FirebaseMessaging.getInstance().subscribeToTopic("news");
            }else{
                FirebaseMessaging.getInstance().unsubscribeFromTopic("news");
            }
            return true;
        });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            startActivity(new Intent(getActivity(), ActivityPref.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Android Studio 报告 NotificationPreferenceFragment 的默认构造函数已弃用。如何修复已弃用的警告?

最佳答案

使用 AndroidX 偏好库。该库管理用户界面并与存储交互,以便您仅定义用户可以配置的个别设置。

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

有关更多详细信息:请关注此 link

关于android - NotificationPreferenceFragment - 默认构造函数已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57826585/

相关文章:

android - 通知通知Android时空对象引用错误 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()'

java - 尝试 parseDouble 时发现异常

android - 如何在 Android 中使用 Fragment 中的按钮调用页面?

android - 工具栏隐藏在嵌套的 PreferenceScreen 中

android - 在 AppWidget Provider 中获取首选项

android - 在 Preference.onSaveInstanceState 中保存什么

android - 在单选组 android 中再次检查单选按钮

android - 我想获取正在运行的应用程序的状态

android - RxJava 作为事件总线?

android - 如何在单击按钮时从一个 fragment 移动到另一个 fragment