Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use PreferenceFragment and PreferenceFragmentCompat?

Tags:

I am just a beginner but i have some questions regarding the support library.

  1. Every time google team add's a new Support version it deprecated the use in new Version, so because of this we have to redo the code again just to support new API level.Can't it be like this , you can have this library but you will be not getting extra features .Instead of just deprecating the entire work ? Because of this there is a lot of extra work to do while making android app.
  2. Why was PreferenceFragment deprecated in Android P and what are the support versions for both libraries PreferenceFragment and PreferenceFragmentCompat also what addition features to do you get?

Android platform is amazing but it's difficult to understand this things . If a developer has made an app what is the guaranty it will work when android releases new version ? With every release something is deprecated either the feature or the whole library.

like image 997
Ravi Parmar Avatar asked Aug 23 '18 08:08

Ravi Parmar


People also ask

How do I use Preferencefragment?

Use PreferenceFragmentCompat to programatically handle preferences. To load the settings into the fragment, load the preferences in the onCreatePreferences() method. To get an instance of a preference, search for a preference using its key through the PreferenceManager within onCreateView() .

What is Preferencefragment?

The preference framework handles showing these other screens from the preference hierarchy. The preference hierarchy can be formed in multiple ways: From an XML file specifying the hierarchy. From different Activities that each specify its own preferences in an XML file via Activity meta-data.

What is preference screen?

Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings. Represents a top-level Preference that is the root of a Preference hierarchy. A PreferenceActivity points to an instance of this class to show the preferences.

How do you display the preference of your application using an activity?

Let us see in this tutorial how to integrate Android's Preference framework ( PreferenceActivity ) in any android app. Create a Hello World Android project in Eclipse. Go to New > Project > Android Project. Give the project name as Android_Preferences_example and select Android Runtime 2.1 or sdk 7.


1 Answers

Every time google team add's a new Support version it deprecated the use in new Version, so because of this we have to redo the code again just to support new API level.

That is not the case. "Deprecated" in Android usually means "we have something that we would prefer that you use". So, while eventually you should try to move off of deprecated APIs, "eventually" could be on the order of a couple of years.

Why was PreferenceFragment deprecated in Android P

The native android.preference.PreferenceFragment was deprecated in Android 9.0, mostly because it inherits from android.app.Fragment, which was deprecated in Android 9.0. The fragment code has had lots of bugs over the years. Google is trying to steer developers towards using a library-supplied fragment implementation, as the libraries can be kept up to date, while older Android devices (unfortunately) do not get updates.

There is nothing stopping you from using android.preference.PreferenceFragment in Android 9.0 if you wish. Ideally, over time, you stop using it, but you do not need to drop everything and change your code tomorrow.

what are the support versions for both libraries PreferenceFragment and PreferenceFragmentCompat

AFAIK android.support.v14.preference.PreferenceFragment should also be marked as deprecated, as it too inherits from android.app.Fragment.

android.support.v7.preference.PreferenceFragmentCompat extends from android.support.v4.app.Fragment, and so AFAIK this is the one that you should be using in the short term.

The whole preference fragment stuff is a bit of a mess at the moment — I am hoping that Google settles this out and provides clearer instructions as part of the migration to androidx over the next few months.

like image 103
CommonsWare Avatar answered Oct 20 '22 17:10

CommonsWare