NotificationPreferenceFragment - 默认构造函数已弃用
NotificationPreferenceFragment - default constructor is deprecated
我的代码是:
@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
我的代码是:
@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