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/
Related
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());
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.
I have developed project using Fragments and Tabs at Bottom,
I use just one activity and all fragemnts and tab click is managed by tabHost and stack of fragment
I use this code github link fragment_tab_study for setting tabs and maintaining stack of fragment
Now the issue is that we just push fragment(i.e replace fragment) so every time it call its onCreateView and all other method, I think this is a very big issue in this demo, as we click second time on tab1 it should only call its onResume method as happen in activity
is any solution for this ?
If you use replace of then in any case you have to start with onCreateView. If you dont want to recycle you can choose add method of transaction. But to replace is better in fragment terminology. You can even save transaction between fragment in back stack.
Please let me know if you have some other concern regarding fragment.
Thanks
Ankur
I am implementing Tab Layout with Swipeable Views in android. For implementation I had followed
AndroidHive
and
Tabs and swipe views.
But with both, I am facing the same problem. I am having 3 fragments but when my application run, onCreateView of 1st and 2nd Fragment called instead of only of 1st fragment's. When I swipe and go to 2nd fragment, onCreateView of 3rd Fragments get called.
So, whatever I code in 2nd fragment execute in the first fragment view. I researched and came to know that it happen to keep the next fragment into memory for smooth animation. But I am wondering, where I would code in the fragment so that it will execute only once or how to restrict Fragment getItem() method to be called only once. What can be the solution for this?
According to the Fragment's documentation you can implement the onCreate() link:
The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
This should fix your problem, because i guess you now only use the onCreateView which might be called more than once.
For more information you can check the Fragment's lifecycle or documentation
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.