FragmentPagerAdapter's Fragment not showing RecyclerView's items - android

I think the easiest way to show the problem is the gif below. When i'm on the home fragment, and switch to Recent, Recent is a fragment with ViewPager and TabLayout, the item that's supposed to be in the recyclerView shows up normally, but when i go back to Home Fragment, and go back to Recent, the Item is not there anymore. I believe that it has something to do with the fact that when I go back to the Recent Fragment, even though the Recent Fragment's onCreate() is being called, the 'All' Fragment, which is the first in the ViewPager is not calling it's onCreate().
It's calling the onResume() however, and i check the RecyclerView's list size in there, and it still has the item in it, it just not showing up.
How can i fix that?
Is there a way to force the FragmentPagerAdapter to call onCreate() on it's fragment's, like the 'All' Fragment when going back?

Found the solution, didn't know where to even look for the problem so i didn't post the code in question to not make it too long.
When using a ViewPager inside a Fragment, don't use getActivity().getSupportFragmentManager() when creating FragmentPagerAdapter, use getChildFragmentManager() instead.
So when it comes to code, use this:
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getActivity(), getChildFragmentManager());
Instead of :
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getActivity(), getActivity().getSupportFragmentManager());

Related

ViewPager Fragment RecycleView Reset

I have 3 fragments in the ViewPager.
PersonalFragment
CropFragment
NotesFragment
Every fragment has a RecyclerView and I was adding items using a dialog in it. However, when I go to the last fragment (i.e. NoteFragment), the first fragment's (i.e. PersonalFragment) RecyclerView gets empty (all items are removed). The same thing happens when I come back to the first fragment, the last fragment's RecyclerView resets (all items are removed).
It is difficult to understand without seeing some of your code which is related. I think you might have implemented the onResume function incorrectly. Each time you go to the third fragment in your ViewPager, the first Fragment needs to be reinitialized and you need to take care of this in your onResume function.
However, another workaround for your problem is setting the off-screen page limit of the ViewPager like the following.
viewPager.setOffscreenPageLimit(2);
Hope that helps.

How to create fragment dynamically in viewpager

when we use fragment in viewpager, we alway add fragment when adapter call the constructor.so when the actvitiy call onCreate(), all the fragment would construct and call onCreate() at the same time.
but I want to construct the fragment when we slide in.what should I do?
For example:
I have 4 fragment: fragment1, fragmeng2, fragment3, fragment4.
at first, the viewpager would create fragment1, and when we slide into next fragment(right slip), the viewpager would create fragment2, and the fragment2 would call it onCreate().
How can I make it?
Thanks a lot for your help,so sorry about my pool English and I don't know have I explain the problem clarify.
Thanks again.
You can just move the creation of the fragment to the getItem() method in your adapter and return the newly created fragment.
My answer might be a bit lacking but I'm sure someone can complete it.
So what you can do is just create one fragment in the onCreate() of the activity. Implement the function onScrollChanged() and try to keep track there with getScrollX() where the user is exactly is in relation to the screen and just make some "if" statements that removes/adds the fragments as you are scrolling to the side.
As you have noted, viewpager actually loads the fragments at once (onCreate and onResume). If your intention is to do something before the next page is displayed, this link might give you an alternate way of doing this.
https://looksok.wordpress.com/2013/11/02/viewpager-with-detailed-fragment-lifecycle-onresumefragment-including-source-code/

Nested Fragments and ViewPager issues on Android

I am using a FragmentStatePagerAdapter in combination with a view pager, which is nested in a fragment on Android. Thus, I am using the ChildFragmentManager in combination with the FragmentStatePagerAdapter to create my fragments.
So far so good, but here is the problem:
If I open a different Activity without finishing the activity that hosts the fragment with the view pager and afterwards resume it, the fragment that was selected in the ViewPager is empty. If I switch the fragment in the ViewPager before I open the different Activity, this issue does not appear. After returning and seeing the empty Fragment in the ViewPager, I am still able to switch the next Fragments, but if I try to switch back to the empty Fragment, I get a NullPointerException. This issue does not occur if I switch between the Fragment hosting the ViewPager and a different Fragment or if I change orientations.
If I use the FragmentManager instead of the ChildFragmentManager, I do not have this issue when switching to a different Activity, but it will then occur if I switch between the hosting and a different fragment or if I change the orientation.
So here's what I already tried without any success:
* Use FragmentStatePagerAdapter adapter and FragmentPagerAdapter
* Use setRetainedInstance(true and false)
* Use getChildFragmentManager() and getFragmentManager()
* Override getItemPosition() return POSTION_NONE and use notifyDataChanged() in onResume()
The only thing that is currently doing the trick is recreating the PagerAdapter in onResume() and re-initializing the ViewPager, but I think that's really bad practice as everything needs to be recreated.
So is there somebody, who can explain to me how a correct implementation should look like, or why the fragments can become empty (they are not null, I checked that). I am searching for a more elegant solution rather than recreating everything each time I resume my activity.
Any help is really appreciated.

ViewPager ,call fragments automatically

Good afternoon!
I have a method which crashed with NullPointerException if I call it immediately after launch , but If i slide through all fragments and then call the method, all works good.
As far as as consider, smth not initialize.
I need to fix this bug fast, so I want to slide through all fragments programatically between UI launch(in onCreateView or smth else). How can I do this ?
Here is RootFragment http://pastebin.com/VfmRbVpc
One of child fragments http://pastebin.com/XnftaqTH
I have a NullPointer in child fragment line 63
elview = (ExpandableListView) v.findViewById(R.id.expand_lv);
Thanks!
Fragments in a ViewPager are created on in a lazily manner so If you need access to a Fragment thats further down the line, you should attach a OnPageChangeListener to the ViewPager and execute the needed code when you reach the target index/position.

Adding Fragment to BackStack using FragmentStatePagerAdapter

I have a FragmentStatePagerAdapter which is used as the ViewPagerAdapter for my ActionBar etc. I am trying to change a fragment and add the previous one to the backstack. It appears, however, that this is not possible.
If I look at the Code it seems as though the only way around this is to destroy the old Fragment then add the new one. This is not optimal for what I am trying to do (multipage form). What I really want to do is take the existing fragment and detach it, then add a new fragment and add it to the backstack.
I have tried this without the ViewPager and it works flawlessly. Short of rewriting my own FragmentStatePagerAdapter class that allows me to replace items in the Fragments List is there anything else I can do?

Categories

Resources