Questions tagged [fragmentpageradapter]

FragmentPagerAdapter is a subclass of the PagerAdapter class from the Android compatibility package that represents each page, in the ViewPager where is used, as a Fragment.

FragmentPagerAdapter represent each page as a Fragment. This adapter class should be used only when there is a limited number of Fragments to swipe in the ViewPager. Using the FragmentPagerAdapter would require at minimum implementing the getItem()(to return the Fragment for this position) and getCount()(to return the number of Fragments in this adapter) methods.


From the documentation of the FragmentPagerAdapter class:

Implementation of PagerAdapter that represents each page as a Fragment that is persistently kept in the fragment manager as long as the user can return to the page.

This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs. The fragment of each page the user visits will be kept in memory, though its view hierarchy may be destroyed when not visible. This can result in using a significant amount of memory since fragment instances can hold on to an arbitrary amount of state. For larger sets of pages, consider FragmentStatePagerAdapter.

When using FragmentPagerAdapter the host ViewPager must have a valid ID set.

Subclasses only need to implement getItem(int) and getCount() to have a working adapter.

Tag Usage:

602 questions
404
votes
9 answers

What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter?

What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter? About FragmentPagerAdapter Google's guide says: This version of the pager is best for use when there are a handful of typically more static fragments to be paged…
197
votes
8 answers

How to implement a ViewPager with different Fragments / Layouts

When I start an activity which implements viewpager, the viewpager created various fragments. I want to use different layouts for each fragment, but the problem is that viewpager shows only two layouts at the max (second layout on all of the…
164
votes
5 answers

FragmentPagerAdapter Exists Only In Android.Support.V4.App (and not Android.App)

I cannot find find FragmentPagerAdapter within Android.App. I do not want to use the Fragment's from Android.Support.V4.App, as my target API is 14 and higher (Android 4.0 and up). So, I want to just use plain of Android.App.Fragments, and there…
samus
  • 6,102
  • 6
  • 31
  • 69
144
votes
17 answers

Remove Fragment Page from ViewPager in Android

I'm trying to dynamically add and remove Fragments from a ViewPager, adding works without any problems, but removing doesn't work as expected. Everytime I want to remove the current item, the last one gets removed. I also tried to use an…
astuetz
  • 2,693
  • 2
  • 23
  • 22
112
votes
14 answers

How to get existing fragments when using FragmentPagerAdapter

I have problem making my fragments communicating with each other through the Activity, which is using the FragmentPagerAdapter, as a helper class that implements the management of tabs and all details of connecting a ViewPager with associated…
Ismar Slomic
  • 5,315
  • 6
  • 44
  • 63
105
votes
7 answers

FragmentPagerAdapter deprecated

Since API 27 FragmentPagerAdapter is deprecated. What's the best alternative to use for this? In my case, I understand something like super(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) would need to be used, but I don't know where within…
wbk727
  • 8,017
  • 12
  • 61
  • 125
58
votes
5 answers

Navigating back to FragmentPagerAdapter -> fragments are empty

I have a Fragment (I'll call it pagerFragment) that is added to the backstack and is visible. It holds a viewPager with a FragmentPagerAdapter. The FragmentPagerAdapter holds (let's say) two fragments: A and B. First adding of the fragments works…
45
votes
6 answers

FragmentPagerAdapter notifyDataSetChanged not working

I got a FragmentPagerAdapter. It's getItem method can return a fragment according to data it has from the outside. After I update the data its suppose to display I call notifyDataSetChanged and yet nothing happened. I DID override the…
CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
41
votes
10 answers

IllegalStateException: The application's PagerAdapter changed the adapter's content without calling PagerAdapter#notifyDataSetChanged

I'm using the ViewPager example with ActionBar tabs taken from the Android documentation here. Unfortunately, as soon as I call the addTab method, the application crashes with the following exception: IllegalStateException: The application's…
41
votes
3 answers

When is FragmentPagerAdapter's getItem called?

I'm writing an application that uses the FragmentPagerAdapter. The fragments in the adapter need to be updated according to outside data - but that doesn't happen. I noticed that the fragment classes are only instantiated once, in the getItem…
n00b programmer
  • 2,671
  • 7
  • 41
  • 56
39
votes
1 answer

ViewPager + FragmentPagerAdapter inside a DialogFragment gets "IllegalArgumentException:No view found..."

I am trying to show a FragmentDialog ( created and shown as a dialog NOT added as content in a view hierarchy) where there is a ViewPager whose content is given by a FragmentPagerAdapter (provides Fragments consisting of an image). The code works…
38
votes
5 answers

FragmentPagerAdapter getItem is not being triggered

Currently, with a FragmentActivity, I toggle among 2 type of Fragments using the following code. private void toggle() { Fragment oldFragment = getSupportFragmentManager().findFragmentById(R.id.content); Fragment fragment = null; if…
36
votes
5 answers

Android getting fragment that is in FragmentPagerAdapter

I have following problem: I have one activity in which I have two tabs which are made both as fragments using FragmentPagerAdapter In some moment I would like to change View of one of these fragments, but I am not able to findFragmentById() or…
ziky90
  • 2,627
  • 4
  • 33
  • 47
31
votes
5 answers

Replacing ViewPager with Fragment - Then Navigating Back

I've got an activity which initially hosts a ViewPager, hooked up to a FragmentPagerAdapter. When the user clicks on an item inside the ViewPager's child fragment, I'm using a FragmentTransaction to replace an empty container view with a new…
29
votes
9 answers

Attempt to invoke virtual method 'android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler()' on a null object reference

My Application consist of 4 fragment as tabs being loaded inside a parent Fragment using FragmentPagerAdapter. The problem is when I run the app and press back and re-open the app I receive this error log: FATAL EXCEPTION:…
1
2 3
40 41