I am new to fragments and had a question: When I updated my SDK tool and went to create a project(min = api8, the default one), I noticed the android-support-v4.jar in my Referenced Library.
When I extended my class and tried to use the Fragment
class, I found out there are 2. One is android.app.Fragment and other is android.support.v4.app.Fragment. What is the difference between these two?
According to the Android documentation, a fragment is a part of applications user interface that is bound to an activity. Fragments have their lifecycle and layouts or UI components. Fragments help enrich your UI design, pass data between different screens, and adapt to different device configurations.
A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.
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.
Android Fragment is the part of activity, it is also known as sub-activity. There can be more than one fragment in an activity. Fragments represent multiple screen inside one activity. Android fragment lifecycle is affected by activity lifecycle because fragments are included in activity.
android.support.v4.app.Fragmentis the Fragmentclass in the android support library, which is a compatibility package that allows you to use some of the newer features of Android on older versions of Android. android.app.Fragmentis the Fragmentclass in the native version of the Android SDK. It was introduced in Android 3 (API 11).
android.app.Fragment is the Fragment class in the native version of the Android SDK. It was introduced in Android 3 (API 11). If you want to make your app use fragments, and want to target devices before API 11, you must use android.support.v4.app.Fragment.
If you want to make your app use fragments, and want to target devices before API 11, you must use android.support.v4.app.Fragment. However, if you're only targeting devices running API 11 or above, you can use android.app.Fragment.
Use the Support Library Fragmentfor consistent behavior across all devices and access to Lifecycle. So support fragments (android.support.v4.app.Fragment) should be used everywhere instead of native fragments(android.app.Fragment) now. Share Follow edited Aug 23 '18 at 11:41
If you're developing for Android 3+, you can use app.Fragment.
However, in 3-, Fragments didn't exist yet. That is where the support library comes in. It provides Fragments for older versions of Android.
See also this link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With