I'm using a ViewPager from ViewPageIndicator and I need to be able to dynamically insert one fragment in the middle of others.
I've tried to manage the model with FragmentPagerAdapter and FragmentStatePagerAdapter (both from v4 support code) and the first seems to don't manage in any way insertion of pages in the middle.
And the second only works if I make a naive implementation of getItemPosition returning always POSITION_NONE but this cause completly recreation of pages every time I swipe.
The problem I've observed with FragmentStatePagerAdapter (FSP) is this:
I start with two pages [A][B]
Then I insert [C] in the middle [A][C][B]. After the insert I call notifyDataSetchange()
Then FSP calls getItemPosition for [A] and gets 0
Then FSP calls geTItemPosition for [B] and gets 2. And it says... Ohhh I've to destroy [B] and makes mFragments.set(2, null) and then as it only have two elements in mFragments array it throws an IndexOutOfBoundsException
After looking a little in the code it seems that provided fragmentStatePagerAdapter doesn't support insertion in the middle. Is that correct or I missing something?
Update:
The insert in the adapter is made in a logical way, when a certain codition is true the pages are incremented by one. The fragment creation is done using constructor in getItem() in that way:
void setCondition(boolean condition) {
this.condition=condition;
notifyDataSetChanged();
}
public int getCount(){
return condition?3:2;
}
public Fragment getItem(int position) {
if(position==0)
return new A();
else if(position==1)
return condition?new C():new B();
else if(position==2 && condition)
return new B();
else throw new RuntimeException("Not expected");
}
public int getItemPosition(Object object) {
if(object instanceof A) return 0;
else if(object instanceof B) return condition?2:1;
else if(object instanceof C) return 1;
}
Solution:
As said in accepted answer the key is to implement getItemId().
Make sure you use at least the R9(June 2012) version of android-support library. Because this method was added in it. Previous to this version that method doesn't exist and adapter doesn't manage correctly insertions.
Also make sure you use FragmentPageAdapter because FragmentStatePagerAdapter still doesn't works because it doesn't use ids.
You forgot about one method.
Override getItemId(int position) from FragmentPagerAdapter which simply returns position to return something what will identify fragment instance.
public long getItemId(int position) {
switch (position) {
case 0:
return 0xA;
case 1:
return condition ? 0xC : 0xB;
case 2:
if (condition) {
return 0xB;
}
default:
throw new IllegalStateException("Position out of bounds");
}
}
Related
I'm using a ViewPager and displaying a lot of different Fragments inside it, not only in content but they use different classes as well. The list to be displayed should be changed dynamically and even though I manage to swap items around and add new ones to the adapter(and calling notifyDataSetChanged), if I try changing the next item it will still slide to it when using mPager.setCurrentItem(mPager.getCurrentItem() + 1);
I am just adding a new Fragment between the current item and the current next one, it is displayed correctly in the adapter but as the next one was already preloaded then getItem in the adapter is not even called.
Is there another method "stronger" than notifyDataSetChanged that tells my ViewPager that it should get the next item again?
CODE SAMPLES:
The add and get item methods inside my FragmentPagerAdapter(only samples, not the actual code)
public void add(#NonNull Integer fragmentIndex) {
mFragmentOrder.add(fragmentIndex);
notifyDataSetChanged();
}
#Override
public Fragment getItem(int position) {
int selectedFragment = mFragmentOrder(position);
Fragment fragment;
switch (selectedFragment) {
case 1:
fragment = new FragmentA();
break;
case 2:
fragment = new FragmentB();
break;
case 3:
fragment = new FragmentC();
break;
default:
fragment = new FragmentD();
break;
}
return fragment;
}
This is the function used to go to the next item(I don't allow swiping)
public void goToNext() {
mPager.setCurrentItem(mPager.getCurrentItem() + 1);
}
EDITS:
Edit 1: I had already tried using a FragmentStatePagerAdapter instead and setting the OffscreenPageLimit to 0, but to no avail.
Edit 2: [Solution] Using a FragmentStatePagerAdapter AND overwriting the getItemPosition function to return POSITION_NONE or the index in the appropriate cases solved the problem. For some reason even after implementing the right version of this function the normal FragmentPagerAdapter kept delivering the wrong Fragment.
By default, FragmentPagerAdapter assumes that the number and positions of its items remain fixed. Therefore, if you want to introduce for dynamism, you have to provide for it yourself by implementing the getItemPosition(Object object) method in the inherited adapter class. A very basic (but unefficient) implementation would be this:
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
Every time the parent view is determining whether the position of one of its child views (items) has changed, this code will force the fragment to be recreated. If you want to avoid the recreation when unnecessary, you have to include some logic in the method. Something like this:
#Override
public int getItemPosition (Object object) {
if (fragmentOrder.indexOf(object) == -1) {
return POSITION_NONE;
} else {
return index;
}
}
Finally, pay attention to possible memory leaks by adding an onDestroyView method to your fragments and nullifying the views you are using.
Here is a good discussion of these issues with the two PagerAdapters.
UPDATE
After some major fighting with this problem and help from SO users I managed to solve it.
This is how my clearProgressUpTo looks like now.
public void clearProgressUpTo(int progress) {
boolean deleted = false;
for (int i = fragments.size() - 1; i > 0; i--) {
int key = fragments.keyAt(i);
if (key != progress) {
fragments.delete(key);
deleted = true;
} else {
break;
}
}
if (deleted)
notifyDataSetChanged(); //prevents recursive call (and IllegalStateException: Recursive entry to executePendingTransactions)
while (mSavedState.size() > progress) {
mSavedState.remove(mSavedState.size()-1);
}
}
The reason why I am doing it after notifyDataSetChanged is because the initiateItem was filling my mSavedState once again. And I am sure that after notifying my adapter does not hold any of these Fragments.
Also if you want to do it like that change your mSaveState to protected so you will be able to get it from the extending class (in my case VerticalAdapter).
I forgot to mention that I am using FragmentStatePagerAdapter fix by Adam Speakman to solve my problem.
/////////////////////////////////////////////////////QUESTION PART//////////////////////////////////////////////////////
the problem that I'm having is that I need to remove completely some of Fragments held in FragmentStatePagerAdapter.
I have already done the POSITION_NONE fix on it but the old fragments seems to still remain intact.
CONSTRUCTION
Because the construction of my ViewPager is unusual I was forced to place Inside my VerticalAdater extending FragmentStatePagerAdapter a SparseArray that holds all my fragments.
public class VerticalAdapter extends FragmentStatePagerAdapter {
private SparseArray<Fragment> fragments = new SparseArray<Fragment>();
...
#Override
public Fragment getItem(int position) {
return getFragmentForPosition(position);
}
#Override
public int getCount() {
return fragments.size();
}
PROBLEM
I have made a special method responsible for clearing all the Fragments up to some point in my FragmentStatePagerAdapter. Its working well but the problem I cant get rid of is clearing the ArrayList inside FragmentStatePagerAdapter. Even tho I clear the SparseArray, the ArrayList of FragmentStatePagerAdapter that my VerticalAdapter extends is still holding the Fragments that I don't want to have there. Because of that when I create new Fragments they have the state of the old ones.
public void clearProgressUpTo(int progress) {
boolean deleted = false;
for (int i = fragments.size() - 1; i > 0; i--) {
int key = fragments.keyAt(i);
if (key != progress) {
fragments.delete(key);
deleted = true;
} else {
break;
}
}
if (deleted)
notifyDataSetChanged(); //prevents recursive call (and IllegalStateException: Recursive entry to executePendingTransactions)
}
If you need more info/code please tell me in the comments I will try to provide as much info as I can.
I have no idea how to fix this problem so any input would be appreciated.
EDIT
Adding requested code.
private Fragment getFragmentForPosition(int position) {
return fragments.get(getKeyForPosition(position));
}
public int getKeyForPosition(int position) {
int key = 0;
for (int i = 0; i < fragments.size(); i++) {
key = fragments.keyAt(i);
if (i == position) {
return key;
}
}
return key;
}
FragmentStatePageAdapter saves the state of the fragment while destroying it, so that latter when the fragment is recreated the saved state will be applied to it.
The fragment state is stored in a private member variable. Hence we can't extend the current implementation to add the feature of removing the saved state.
I've cloned the current FragmentStatePagerAdapter implementation and have included the saved state removal functionality. Here is the gist reference.
/**
* Clear the saved state for the given fragment position.
*/
public void removeSavedState(int position) {
if(position < mSavedState.size()) {
mSavedState.set(position, null);
}
}
In your clearProgressUpTo method, whenever you remove the fragment from sparse array, call this method, which clears off the saved state for that fragment.
Your problem is not caused by your use of SparseArray. In fact, you meet a bug of the FragmentStatePagerAdapter. I spent some time reading the source code of FragmentStatePagerAdapter, ViewPager and FragmentManagerImpl and then found the reason why your new fragments have the state of old ones:
The FragmentStatePagerAdapter save the sate of your old fragments when it remove them, and if you add new fragments to you adapter later, the saved state will be passed to your new fragment at the same location.
I googled this problem and find a solution, here is its link:
http://speakman.net.nz/blog/2014/02/20/a-bug-in-and-a-fix-for-the-way-fragmentstatepageradapter-handles-fragment-restoration/
The source code can be found here:
https://github.com/adamsp/FragmentStatePagerIssueExample/blob/master/app/src/main/java/com/example/fragmentstatepagerissueexample/app/FixedFragmentStatePagerAdapter.java
The key idea of this solution is to rewrite the FragmentStatePagerAdapter and use a String tag to identify each fragment in the adapter. Because each fragment has a different tag, it is easy to identify a new fragment, then don't pass a saved state to a new Fragment.
Finally, say thanks to Adam Speakman who is the author of that solution.
When you call notifyDataSetChanged() the adapter starts removing its fragments using destroyItem(ViewGroup container, int position, Object object).
However, each fragment's instance state is retained internally by FragmentStatePagerAdapter to be later used in instantiateItem(ViewGroup container, int position).
Simply save item's position you want to remove and call notifyDataSetChanged(). Just after the last item is destroyed, retrieve the desired saved instance state from FragmentStatePagerAdapter and replace with null.
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
if(position == getCount() - 1 && i >= 0){
Bundle state = (Bundle) saveState();
Fragment.SavedState[] states = (Fragment.SavedState[]) state.getParcelableArray("states");
if (states != null)
states[i] = null;
i = -1;
restoreState(state, ClassLoader.getSystemClassLoader());
}
Note: getItemPosition(Object object) must return PagerAdapter.POSITION_NONE.
I understand that the offscreen page limit for a viewpager must be at least 1. I am trying to dynamically change the fragments in my viewpagers as I am constantly grabbing information from a server.
For example, I have Fragments A, B, C instantiated when I am viewing Fragment B.
I want to change Fragment A's info, so I update it and call notifyDataSetChanged(). I have not created a new Fragment and inserted it in its place, but changed the imageview associated with the original fragment.
However, once I try to navigate to A, I run into an error saying "Fragment is not currently in the FragmentManager "
Can anyone explain to me how I'd be able to jump back and forth between immediate pages in a viewpager while allowing these pages to change their views?
I didn't do that, but my suggestion will be not to try to. Android does a lot of magic under the hood, and it is very possible you'll face a lot of issues trying to implement what you want. I had an experience with ListView when I was trying to save contentView for each list item, so that Android will render them only once, after the whole day I gave up the idea because every time I've changed the default behavior, something new came up (like exceptions that views are having two parents). I've managed to implement that, but the code was really awful.
Why don't you try, for example, save the image you've downloaded on the disc, and retrieve if when fragment actually appears on the screen ? Picasso library could help in this case
To do that, you should create a FragmentPagerAdapter that saves references to your pages as they are created. Something like this:
public class MyPagerAdapter extends FragmentPagerAdapter {
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
return MyListFragment.newInstance(TAB_NAME_A);
case 1:
return MyListFragment.newInstance(TAB_NAME_B);
case 2:
return MyListFragment.newInstance(TAB_NAME_C);
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return TOTAL_PAGES;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}
You can access a particular page like this:
((MyListFragment) myPagerAdapter.getRegisteredFragment(0)).updateUI();
Where updateUI() is a custom method that updates your list on that page and calls notifyDataSetChanged().
I have a ViewPager with 3 Fragments and my FragmentPagerAdapter:
private class test_pager extends FragmentPagerAdapter {
public test_pager(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
return fragments[i];
}
#Override
public long getItemId(int position) {
if (position == 1) {
long res = fragments[position].hashCode()+fragment1_state.hashCode();
Log.d(TAG, "getItemId for position 1: "+res);
return res;
} else
return fragments[position].hashCode();
}
#Override
public int getCount() {
return fragments[2] == null ? 2 : 3;
}
#Override
public int getItemPosition(Object object) {
Fragment fragment = (Fragment) object;
for (int i=0; i<3; i++)
if (fragment.equals(fragments[i])){
if (i==1) {
return 1; // not sure if that makes a difference
}
return POSITION_UNCHANGED;
}
return POSITION_NONE;
}
}
In one of the page (#1), I keep changing the fragment to be displayed. The way I remove the old fragment is like this:
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().remove(old_fragment1).commit();
And then just changing the value of fragments[1]
I found that I cannot really add or replace the new one or it will complain the ViewPager is trying to add it too with another tag... (am I doing something wrong here?)
All the fragments I display have setRetainInstance(true); in their onCreate function.
My problem is that this usually works well for the first few replacement, but then when I try to reuse a fragment, sometimes (I have not really figured out the pattern, the same fragment may be displayed several times before this happens) it will only show a blank page.
Here is what I have found happened in the callback functions of my Fragment I am trying to display when the problem happens:
onAttach is called (but at that time, getView is still null)
onCreateView is not called (that's expected)
onViewStateRestored is not called (why not?)
onResume is not called (I really thought it would...)
If it changes anything, I am using the support package, my activity is a SherlockFragmentActivity
EDIT (to answer Marco's comment):
The fragments are instantiated in the onCreate function of the Activity, I fill an ArrayList with those fragments:
char_tests = new ArrayList<Fragment>(Arrays.asList(
new FragmentOptionA(), new FragmentOptionB(), new FragmentOptionC()));
The I pick from that list to set fragments[1] (that's all done in the UI thread)
I fixed this by changing test_pager to extends FragmentStatePagerAdapter instead.
I am still confused as to what PagerAdapter should be used depending on the usage. The only thing I can find in the documentation says that FragmentPagerAdapter is better for smaller number of pages that would be kept in memory and FragmentPagerStateAdapter better for a larger number of pages where they would be destroyed and save memory...
When trying to do (fancy?) things with Fragments, I found FragmentStatePagerAdapter is better when pages are removed and re-inserted like in this case. And FragmentPagerAdapter is better when pages move position (see bug 37990)
I have a viewpager that pages through fragments. My FragmentPagerAdapter subclass creates a new fragment in the getItem method which seems wasteful. Is there a FragmentPagerAdapter equivalent to the convertView in the listAdapter that will enable me to reuse fragments that have already been created? My code is below.
public class ProfilePagerAdapter extends FragmentPagerAdapter {
ArrayList<Profile> mProfiles = new ArrayList<Profile>();
public ProfilePagerAdapter(FragmentManager fm) {
super(fm);
}
/**
* Adding a new profile object created a new page in the pager this adapter is set to.
* #param profile
*/
public void addProfile(Profile profile){
mProfiles.add(profile);
}
#Override
public int getCount() {
return mProfiles.size();
}
#Override
public Fragment getItem(int position) {
return new ProfileFragment(mProfiles.get(position));
}
}
The FragmentPagerAdapter already caches the Fragments for you. Each fragment is assigned a tag, and then the FragmentPagerAdapter tries to call findFragmentByTag. It only calls getItem if the result from findFragmentByTag is null. So you shouldn't have to cache the fragments yourself.
Appendix for Geoff's post:
You can get reference to your Fragment in FragmentPagerAdapter using findFragmentByTag(). The name of the tag is generated this way:
private static String makeFragmentName(int viewId, int index)
{
return "android:switcher:" + viewId + ":" + index;
}
where viewId is id of ViewPager
Look at this link: http://code.google.com/p/openintents/source/browse/trunk/compatibility/AndroidSupportV2/src/android/support/v2/app/FragmentPagerAdapter.java#104
Seems a lot of the people viewing this question are looking for a way to reference the Fragments created by FragmentPagerAdapter/FragmentStatePagerAdapter. I would like to offer my solution to this without relying on the internally created tags that the other answers on here use.
As a bonus this method should also work with FragmentStatePagerAdapter. See notes below for more detail.
Problem with current solutions: relying on internal code
A lot of the solutions I've seen on this and similar questions rely on getting a reference to the existing Fragment by calling FragmentManager.findFragmentByTag() and mimicking the internally created tag: "android:switcher:" + viewId + ":" + id. The problem with this is that you're relying on internal source code, which as we all know is not guaranteed to remain the same forever. The Android engineers at Google could easily decide to change the tag structure which would break your code leaving you unable to find a reference to the existing Fragments.
Alternate solution without relying on internal tag
Here's a simple example of how to get a reference to the Fragments returned by FragmentPagerAdapter that doesn't rely on the internal tags set on the Fragments. The key is to override instantiateItem() and save references in there instead of in getItem().
public class SomeActivity extends Activity {
private FragmentA m1stFragment;
private FragmentB m2ndFragment;
// other code in your Activity...
private class CustomPagerAdapter extends FragmentPagerAdapter {
// other code in your custom FragmentPagerAdapter...
public CustomPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// Do NOT try to save references to the Fragments in getItem(),
// because getItem() is not always called. If the Fragment
// was already created then it will be retrieved from the FragmentManger
// and not here (i.e. getItem() won't be called again).
switch (position) {
case 0:
return new FragmentA();
case 1:
return new FragmentB();
default:
// This should never happen. Always account for each position above
return null;
}
}
// Here we can finally safely save a reference to the created
// Fragment, no matter where it came from (either getItem() or
// FragmentManger). Simply save the returned Fragment from
// super.instantiateItem() into an appropriate reference depending
// on the ViewPager position.
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
// save the appropriate reference depending on position
switch (position) {
case 0:
m1stFragment = (FragmentA) createdFragment;
break;
case 1:
m2ndFragment = (FragmentB) createdFragment;
break;
}
return createdFragment;
}
}
public void someMethod() {
// do work on the referenced Fragments, but first check if they
// even exist yet, otherwise you'll get an NPE.
if (m1stFragment != null) {
// m1stFragment.doWork();
}
if (m2ndFragment != null) {
// m2ndFragment.doSomeWorkToo();
}
}
}
or if you prefer to work with tags instead of class member variables/references to the Fragments you can also grab the tags set by FragmentPagerAdapter in the same manner:
NOTE: this doesn't apply to FragmentStatePagerAdapter since it doesn't set tags when creating its Fragments.
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
// get the tags set by FragmentPagerAdapter
switch (position) {
case 0:
String firstTag = createdFragment.getTag();
break;
case 1:
String secondTag = createdFragment.getTag();
break;
}
// ... save the tags somewhere so you can reference them later
return createdFragment;
}
Note that this method does NOT rely on mimicking the internal tag set by FragmentPagerAdapter and instead uses proper APIs for retrieving them. This way even if the tag changes in future versions of the SupportLibrary you'll still be safe.
Don't forget that depending on the design of your Activity, the Fragments you're trying to work on may or may not exist yet, so you have to account for that by doing null checks before using your references.
Also, if instead you're working with FragmentStatePagerAdapter, then you don't want to keep hard references to your Fragments because you might have many of them and hard references would unnecessarily keep them in memory. Instead save the Fragment references in WeakReference variables instead of standard ones. Like this:
WeakReference<Fragment> m1stFragment = new WeakReference<Fragment>(createdFragment);
// ...and access them like so
Fragment firstFragment = m1stFragment.get();
if (firstFragment != null) {
// reference hasn't been cleared yet; do work...
}
If the fragment still in memory you can find it with this function.
public Fragment findFragmentByPosition(int position) {
FragmentPagerAdapter fragmentPagerAdapter = getFragmentPagerAdapter();
return getSupportFragmentManager().findFragmentByTag(
"android:switcher:" + getViewPager().getId() + ":"
+ fragmentPagerAdapter.getItemId(position));
}
Sample code for v4 support api.
For future readers!
If you are thinking of reusing fragments with viewpager, best solution is to use ViewPager 2, since View Pager 2 make use of RecyclerView.
Medium article - Exploring the View Pager 2
Docs
Samples repo
Release notes
I know this is (theoretically) not an answer to the question, but a different approach.
I had an issue where I needed to refresh the visible fragments. Whatever I tried, failed and failed miserably...
After trying so many different things, I have finally finish this using BroadCastReceiver. Simply send a broadcast when you need to do something with the visible fragments and capture it in the fragment.
If you need some kind of a response as well, you can also send it via broadcast.
cheers