Preferences without deprecated methods - android

I'm trying to (correctly) implement a preferences screen, but the problem is that all the methods used to read preferences from xml files are deprecated (or I just don't recognize them). The official sample code on the dev site (PreferenceActivity) uses deprecated methods. Has anyone found out a way to implement a preferences screen with an xml file but without using either: addPreferencesFromResource(int) or findPreference(CharSequence)? Or have the methods just been marked deprecated without implementing the alternative yet?
EDIT: Developing for Android version 2.1

Why its deprecated and what is the alternative is pretty well explained in documentation:
This is the base class for an activity to show a hierarchy of preferences to the user. Prior to HONEYCOMB this class only allowed the display of a single set of preference; this functionality should now be found in the new PreferenceFragment class. If you are using PreferenceActivity in its old mode, the documentation there applies to the deprecated APIs here.
In other words, if you want to be HONEYCOMB compliant, then you should use PreferenceFragment for your PreferenceActivity. A detailed explanation on how to use fragments can be found in dev guide.

In Android 3, API Level 11, the fragment-based preference model was introduced, thus deprecating methods that "is not relevant for a modern fragment-based PreferenceActivity."
Since the online reference is the latest version, it shows the methods as deprecated. By manipulating the API Level dropdown, you can mark the methods that are not in the given Android version, but it doesn't update the descriptions to match, which is why it still shows up as deprecated.
If you don't plan on supporting Android 3+ you should just use the old methods, as the fragment-based solutions will not work to versions prior to this.

Related

What is the best way to create a multi-choice list in application settings on Android 10 and higher?

I'm working on a simple news app based on a predefined list of RSS sources. I want to give the users the choice, to choose which sources they want to use. I'm thinking about a ListPreference in the settings menu, containing the available sources, but with a multi-choice nature, rather than the default single choice. (as the official Material Design guide suggests for multi-choice setting entries).
I found that MultiSelectListPreference exists, and is exactly what I need. However this class is deprecated since API level 29. Sadly, the official page does not give enogh information about what should be used now.
What is the recommended way to create a similar functionality as MultiSelectListPreference in API level 29 and higher?
The replacement is MultiSelectListPreference from the AndroidX preference library. See this guide for how to use that library.

When to use PreferenceFragment and PreferenceFragmentCompat?

I am just a beginner but i have some questions regarding the support library.
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.
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.
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.

Why does Kit Kat require the use of the isValidFragment?

Ever since KitKat was released, I've noticed a whole bunch of my apps updating with "Fixing a crash in Kit Kat". Recently when I released my own app, I figured out the likely source of that is the new "isValidFragment" requirement for using preference activities. I haven't been able to get anyone, however, to explain why this new class is suddenly needed to validate fragments. Can anyone offer me an explanation of why this is required?
Subclasses should override this method and verify that the given fragment is a valid type to be attached to this activity. The default implementation returns true for apps built for android:targetSdkVersion older than KITKAT. For later versions, it will throw an exception.
A New Vulnerability in the Android Framework: Fragment Injection
We have recently disclosed a new vulnerability to the Android Security
Team. The vulnerability affected many apps, including Settings (the
one that is found on every Android device), Gmail, Google Now, DropBox
and Evernote. To be more accurate, any App which extended the
PreferenceActivity class using an exported activity was automatically
vulnerable. A patch has been provided in Android KitKat. If you
wondered why your code is now broken, it is due to the Android KitKat
patch which requires applications to override the new method,
PreferenceActivity.isValidFragment, which has been added to the
Android Framework.
http://securityintelligence.com/new-vulnerability-android-framework-fragment-injection/
http://securityintelligence.com/wp-content/uploads/2013/12/android-collapses-into-fragments.pdf
Here: http://commonsware.com/blog/2013/12/13/sanitize-all-the-extras.html it is suggested that this was introduced as a security fix:
PreferenceActivity supports extras to load specific
PreferenceFragments into the activity. This is used heavily by the
Settings app, to allow apps to drive straight into particular screens
(actually fragments). Unfortunately, there was no logic in
PreferenceActivity to ensure that only those fragments that were
supposed to be externally reachable were loaded via these extras —
hence, the addition of isValidFragment(). So, a properly-crafted
Intent can open any exported PreferenceActivity and launch any
PreferenceFragment from it, in the absence of such defenses.
(bold text added by me)
You got this documented:
Subclasses should override this method and verify that the given
fragment is a valid type to be attached to this activity. The default
implementation returns true for apps built for
android:targetSdkVersion older than KITKAT. For later versions, it
will throw an exception.
so as long as your targetSdk is below 19, you do not need to care. If it is 19 then your app will crash due to the exception, unless you implement isValidFragment()..
Taken from a blog of commonsware.
Once you target API Level 19 or higher, you will need to override isValidFragment() in your PreferenceActivity, to validate that the supplied fragment class name is indeed something that should be displayed. Off the cuff, this feels like some hack to deal with a security flaw.
Documentation says
protected boolean isValidFragment (String fragmentName)
Subclasses should override this method and verify that the given fragment is a valid type to be attached to this activity. The default implementation returns true for apps built for android:targetSdkVersion older than KITKAT. For later versions, it will throw an exception.

"The type TabActivity is deprecated" For app tab

"The type TabActivity is deprecated"?
I am making the Tabs of app following tutorial book.
I've checked from the android developer.com website, but i have no ideas on the significance of the following message : This class is deprecated.
New applications should use Fragments instead of this class; to continue to run on older devices, you can use the v4 support library which provides a version of the Fragment API that is compatible down to DONUT."* (http://developer.android.com/reference/android/app/TabActivity.html)
What is v4 support library?
How to finish the tab functions?
You can still use a deprecated package. It is however recommended to use Fragments, and thus the support package. You can read more about it here. However, if you are a beginner at java and android development, I would recommend ignoring the deprecation for now and come back to this when you have completed the tutorial you are currently using if you find it educating.
If you want to watch a nice example of tabbed navigation using Fragments, then create a new project in Eclipse using android 4.0 or later. Make sure your android-plugin is updated. You will get the option to create a project with basic navigation already implemented.
"Deprecated" means that the api developers don't recommend using it anymore, probably because its not a good model, or inefficient, etc. Fragments were introduced in Honeycomb and can be used to provide a similar functionality as tabs and is more in-line with android's current design philosophy.
Since Fragment was introduced in Android 3.0 Honeycomb, you might think you cannot use that for pre-Honeycomb devices. Enter Support Libraries. They are libraries which you can include in your application which needs to run on pre-Honeycomb and still use this class.
So if you want to, you can finish the TabActivity as described in whatever tutorial you are following, it'll probably work on a few more upcoming android versions. But it is recommended that you start using Fragments.

How to add Preference in ICS

I need to add Preference in ICS using addPreferencesFromResource().
It is shown as deprecated in ICS.
I've added a preference layout in xml folder but this could not be added to the Preference activity.
How can I add it?
On API Level 11 and higher, the plan is for you to use PreferenceFragments to call addPreferencesFromResource(). You can see an example of this in the documentation for PreferenceActivity.
If you are trying to support PreferenceFragment on API Level 11+, yet still support older devices, you will need to do both the stuff shown in that documentation and call addPreferencesFromResource() directly in your PreferenceActivity... but only when you are on an older device. Even though addPreferencesFromResource() is marked as deprecated, that does not mean that it does not work, and more importantly it is your only option on API Level 10 and below.
Here is a sample project where I demonstrate supporting both API Level 11-style fragments and the classic PreferenceActivity from a single set of source.

Categories

Resources