Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragments deprecated in Android P

I was looking at the documentation and found this

This class was deprecated in API level P.

Why are fragments deprecated in android P?

like image 951
RoyalGriffin Avatar asked Mar 23 '18 09:03

RoyalGriffin


People also ask

Are Android fragments deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle. This class was deprecated in API level 28.

Are fragments still used in Android?

Using the support library, fragments are supported back to all relevant Android versions. Fragments encapsulate views and logic so that it is easier to reuse within activities.

Is Fragment Manager deprecated?

This class was deprecated in API level 28. Use the Support Library androidx.

In which Android version does fragments are introduced?

A fragment can implement a behaviour that has no user interface component. Fragments were added to the Android API in Honeycomb version of Android which API version 11.


2 Answers

The rewrite in Support Library 27.1.0

Ian's medium post (Feb 28, 2018) gives us an explanation about it. He is Android Framework Developer at Google.

Loaders in Support Library 27.1.0

For Support Library 27.1.0, I rewrote the internals of LoaderManager, the class powering the Loaders API and I wanted to explain the reasoning behind the changes and what to expect going forward.

Loaders and Fragments, a history
From the beginning, Loaders and Fragments were pretty tightly tied together at the hip. This meant that a lot of the code in FragmentActivity and Fragment were there to support Loaders, despite the fact that there are indeed fairly independent. …

What’s changed in 27.1.0
With 27.1.0, the technical debt of Loaders has been greatly reduced: …

Note: Obviously, these changes only apply to Support Library Loaders. If you are using Android framework Loaders, please switch to the Support Library Loaders as soon as possible. There are no bug fixes or improvements planned for the framework Loader APIs.

It seems like the code in Fragment and FragmentActivity has been refactored in order to make Loaders an optional dependency.

According to the release note, the new implementation is based on Lifecycle.

Important Changes
The underlying implementation of Loaders has been rewritten to use Lifecycle.

Architecture Components

In Support Library 26.1.0, Fragment and FragmentActivity has adopted Lifecycle.

This is a special release to integrate the Support Library with Lifecycles from Architecture Components. If you are not using the Lifecycles library, you don’t need to update from 26.0.2. For more information, see the Architecture Components release notes.

Important changes

  • Fragment and FragmentActivity (the base class for AppCompatActivity) now implement the LifecycleOwner interface from Architecture Components.

By contrast, Fragment and Activity in Android P have not implemented the interface LifecycleOwner.

In the Google+ post (mentioned in ThanosFisherman’s answer), Ian made a comment:

you can't change framework code after it has shipped - it is literally frozen in time. That means no new features and more importantly no bug fixes. That's not a good developer experience, particularly when we do have a fully supported, up to date, backward compatible version in the Support Library.

I think that’s the reason why Android P doesn't adopt Lifecycle. Consequently Fragment is deprecated in Android P.

like image 120
qtmfld Avatar answered Sep 19 '22 14:09

qtmfld


In case anyone was looking for the way to instantiate fragments by the class name.

Old way:

Fragment.instantiate(context, fragmentClass)

New way:

val fm: FragmentManager = ... fm.fragmentFactory.instantiate(ClassLoader.getSystemClassLoader(), fragmentClass) 

Using an extension:


File name: FragmentManagerExt.kt

import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager  fun FragmentManager.instantiate(className: String): Fragment {     return fragmentFactory.instantiate(ClassLoader.getSystemClassLoader(), className) } 

Example usage:

val fragment = supportFragmentManager.instantiate(fragmentClassName) 
like image 42
Markymark Avatar answered Sep 18 '22 14:09

Markymark