What use instead of deprecated android.preference library on android 10+?
Asked Answered
H

2

6

I start new tv app and now I need to develop dynamic preferences for set quality of stream, language of stream etc. So I was planning to use android.preference library and dynamically generate perfs base on stream data. But on https://developer.android.com/guide/topics/ui/settings.html is

Note: This guide explains how to use the AndroidX Preference library. Starting with Android 10, the platform android.preference library is deprecated.

But there is no redirection to replacement. So what to use in place of android.preference library?

Also on that guide is also note:

Note: This guide assumes you are using androidx.preference:preference:1.1.0-alpha04 or higher. Some features might be unavailable on older versions of the library.

so androidx.preference:preference will be deprecated.

Hoarding answered 10/9, 2019 at 7:42 Comment(0)
H
9

the platform android.preference library is deprecated. But there is no redirection to replacement.

You can check in Android 10 documentation:
The android.preference library is deprecated as of Android 10. Developers should instead use the AndroidX preference library, part of Android Jetpack.

The replacement is the androidx-preference library.
Just use:

dependencies {
    def preference_version = "1.1.0"

    // Java
    implementation "androidx.preference:preference:$preference_version"
    // Kotlin
    implementation "androidx.preference:preference-ktx:$preference_version"
}

More info about the release notes.

Also:

Note: This guide assumes you are using androidx.preference:preference:1.1.0-alpha04 or higher. Some features might be unavailable on older versions of the library.

It means that while a release is in alpha, APIs may be added, removed, or changed.

Haskins answered 10/9, 2019 at 7:46 Comment(4)
it seems that what you pointed will be deprecated. See updated question.Hoarding
@Hoarding There is a misunderstanding. Androidx-preference library is quite new and it will replace the android preference. You can check the documentation of the old android.preference platform. These classes are deprecated. Use the AndroidX Preference Library for consistent behavior across all devices. Also the stable version 1.1.0 was released a few days agoHaskins
Also you can check this link in Android 10 changes. I've just updated the answer.Haskins
The AndroidX variant won't be deprecated at this time, most likely.Steamheated
E
1

To clarify the steps (based on @GabrieleMariotti's helpful answer) to upgrade to use the AndroidX Preference library:

  • To your build.gradle (Module:app) add the following to dependencies

    dependencies {
    ...
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation "androidx.preference:preference:1.1.0"
    ...
    }
    
  • I also kept the xmlns:android url in res/xml/my_preferences.xml, despite the documentation suggesting otherwise. That is, I have

    <PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <PreferenceCategory android:title="Individual">
            <Preference
                android:key="preference_play_activity"
                android:title="Play Activity">
                <intent
                    android:targetClass="com.myorg.mythirdapp.PlayActivity"
                    android:targetPackage="com.myorg.mythirdapp" />
            </Preference>
            ...
    

    ... rather than ...

    <PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
    
Euphorbiaceous answered 16/11, 2019 at 1:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.