Android.app Fragments vs. android.support.v4.app using ViewPager?
You can use ViewPager
with native fragments from the android.app
package with the adapters from the android.support.v13.app package. You have to use the v13 support jar for that.
There are two versions of the adapters that work with ViewPager
, the ones in the v4
package are meant to be used with support fragments, the ones in v13
with native fragments.
The reason why there are now two fragment implementations is historical: Fragments in the android.app
package were introduced with Android 3 for tablets only and the support library was created to bring fragments to phones running older versions. On Android 4 you have both.
From my own experience I would recommend using support fragments, even when developing for Android 4. Here are some reasons: Fragment or Support Fragment?
Confused by android.support.v4.app.FragmentActivity and android.app.FragmentTransaction
All the apps I write need to support right back to Android 2.3 and this is the easiest way to do it.
If you're supporting 11+ then stick to android.app.Fragment
.
android.support.v4.app.Fragment
is the Fragment class 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.support.v4.app.Fragment
is a library that you can use to get backwards-compatibility for older API version.
android.app.Fragment
is the Fragment class in the native version of the Android SDK. It was introduced in 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're only targeting devices running API 11 or above, you can use android.app.Fragment
.
Always use v4 Fragments instead of the build-in android.app.Fragment?
I just realized that it is possible to use the android.app.Fragment
and the android.support.v4.view.ViewPager
together... If you use the android.support.v13.app.FragmentPagerAdapter.
.. So it seems that the v13 support package solves/improves the compatibility issues between android.app.Fragment and android.support.v4.view.ViewPager
. What a mess.
While this resolves the particular example I came up with, I am still wondering if the best option is to always use v4 classes when they are available instead of the build-in classes (e.g. android.app.Fragment) even if I am only worried about ICS and newer devices?
FragmentPagerAdapter android.support.v4.app.fragment can not be applied to android.ap.Fragment
FragmentPagerAdapter android.support.v4.app.fragment can not be applied to android.ap.Fragment
When you are using android.support.v4.app.FragmentManager then you
should use getSupportFragmentManager() and if you are using
android.app.FragmentManager then call getFragmentManager()
You are using import android.support.v4.app.Fragment;
Don't
viewPager.setAdapter(new HomePage_Slider(getActivity().getFragmentManager()));
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Do
viewPager.setAdapter(new HomePage_Slider(getActivity().getSupportFragmentManager()));
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
You should call getSupportFragmentManager ()
instead of getFragmentManager()
.
Return the FragmentManager for interacting with fragments associated
with this activity.
Inconsistency errors between android.support.v4.app and android.app
You must import Fragment and FragmentManager class from support library please change your first two lines of import like below in your ScreenSlideActivity class
import android.support.v4.app.FragmentManager
import android.support.v4.app.Fragment
FragmentPagerAdapter Exists Only In Android.Support.V4.App (and not Android.App)
There is one that is in android.support.v13.app.FragmentPagerAdapter
, which should do what you want it to do. It's a FragmentPagerAdapter for non-support fragments.
Android Studio Installation
Please add follow Gradle dependencies
dependencies {
compile 'com.android.support:support-v13:+'
}
Related Topics
Navigation Drawer: Set as Always Opened on Tablets
Difference Between APK (.Apk) and App Bundle (.Aab)
Error :: Duplicate Files During Packaging of APK
Get Cursor Position in Android in Edit Text
Trying to Get the Display Size of an Image in an Imageview
What Is the Use of the Res/Values/Public.Xml File on Android
How to Animate a View with Translate Animation in Android
Fragments Onresume from Back Stack
Android Recyclerview:Notifydatasetchanged() Illegalstateexception
Android - Custom UI with Custom Attributes
Android - Configure Spinner to Use Array
How Is Staticlayout Used in Android
How to Draw a Route, Along an Existing Road, Between Two Points
Android Speech Recognition Continuous Service
Completely Transparent Status Bar and Navigation Bar on Lollipop
How to Add Shortcut to Home Screen in Android Programmatically
Fragment in Viewpager Using Fragmentpageradapter Is Blank the Second Time It Is Viewed