Fragment initialized without onCreate being called - android

So I have a fragment inside a viewpager which is contained inside a fragment which is getting initialize'd as shown in the debugger but doesn't have it's onCreate,onCreateView or any such methods being called. The activity containing has a bottom navigation view and contains 4 such fragments and this issue is only happening in 1 such fragment.
All these fragments and viewpagers and fragments inside them are created on the oncreate of the activity. If I move the logic to create the fragment to when a bottom tab is clicked, this issue gets solved on.
How is this possible ?

Only the currently active fragment and the two directly adjecant fragments are created when the viewpager is first shown. so the fragment for the first page, the fragment for the page shown if you "scroll to the left" and the fragment for the page shown when you "scroll to the right". the 4th fragment will be created when it is put in the next adjecant position.
so if you have fragments a,b,c,d in a viewpager like -a-[b]-c-d- where b is the first visible page, only a,b and c will be created at startup. when you scroll to c -a-b-[c]-d- onCreate for fragment d will be called.

You can control number of pages/fragments preloaded when you launch your activity by setting it's off Screen Page Limit on your view pager adapter. like this
mViewPager.setOffScreenPageLimit(limit)
If you set limit to 0 than only first fragment would be load.
https://spotandroid.com/2016/11/23/android-tricks-viewpager-and-offscreen-tabs/
https://techcodegeek.wordpress.com/2015/06/23/android-viewpager-and-performance-improvements/

Need more detail to know your issue.Issue which you are facing is not understood with information you have provided.

Related

manage life cycle of fragments that use in SlidingTabLayout

in my app i use SlidingTabLayout to make tabs ,my app has three tabs for each tab attach with fragment hence i have three classes extend from Fragment when Run App
show me onCreateView and OnCreate of first and second fragment created together i want to when click on second tab onCreateView and OnCreate of second tab created
because i load things from webservice when click second tab
like facebook app that happen loading things when click on tab
Looks like you're using SlidingTabLayout combined with ViewPager. ViewPager by default manages 3 items (curent one on one on the "left"/"right" side) one of which is displayed and 2 others are in idle state. You can't decrease number of such items so the only way for you is to move your web service call from onCreate/onViewCreated to some other place. For example you may call setHasOptionsMenu(true) in your second fragment and implement your web service call in onOptionsMenuCreated callback of your second fragment.
There is a predefined method called setOffscreenPageLimit in view pager,
setOffscreenPageLimit(int limit)
Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.
For eg.
ViewPager.setOffscreenPageLimit(3);

How to save some value and destroy a fragment after swiping?

I have several fragments. I want them to come one after another as ViewPager. If i swipe right then a fragment will be destroyed storing a value "yes" and if i swipe left it will destroy storing a value "no" and next fragment will appear. I tried FragmentStatePagerAdapter but not getting desired result. I have google a lot but unable to do as I want.
Suppose I have 3 fragments 1,2,3.
1 is the first fragment. If I swipe right/left fragment 1 will destroy storing a value and fragment 2 will appear, and so on. When swiping to last fragment, a Dialog box will pop up with completion message.
Your ViewPager does not destroy your fragment, when you swipe from fragmet 1 to fragment 2. However, if you swipe from fragment 2 to fragment 3, fragment 1 is destroyed. This also applies to when you swipe from fragment 2 to fragment 1. In that case, fragment 3 is destroyed.
If you want to store a value between swipes, you should probably use the onPause callback method of the respective fragment.

Android - Viewpager doesn't display content

I have a fragment that hosts a ViewPager with 3 fragments, 2 of them contain ListView (not inherits FragmentList)
The problem is:
If I click on one of the items in one of the list the application navigates me correctly to a new fragment, but, if I press the back button and go back to the ViewPager, a can see a blank fragment, only the first fragment who didn't have ListView is created, none of the other fragments are displayed, nor even their onStart is called
What's wrong? I suppose the fragment is not attached again, how can I solve this issue?

Fragment doesn't refresh content after returning from back stack

I have a ZooFragment which contains a ViewPager.
This ViewPager has three children: LionFragment, LeopardFragment, and TigerFragment, each of these children can request transaction to call a new ZooFragment.
When a ZooFragment called zooA (with arguments) is initialized, all three children in ViewPager display content. From any child fragment, user touch will call a new ZooFragment called zooB (with different arguments, of course).
Based on transaction action from child fragment to ZooFragment:
1.If I use transaction.replace(), zooB will be blank, all three children in zooB display no content, empty. At zooB, pressing hardkey Back from navigation, zooA becomes empty.
2.If I use transaction.add(), zooB won't be blank, following by a Back button press, zooA gets empty.
In ZooFragment class, I do loading data in onCreateView(), so what is the reason why all the child fragments in ViewPager get empty?
Please do not replace the fragments in the ViewPager, just show (using transaction) in any other contained in the same layout as that of ViewPager and that container should be a FrameLayout. Also device a mechanism to know when and what fragment comes into view. Then request for a fresh data from a utility that responds to update your fragment. I do not think onDestroyed will be called in case of all the fragments in the ViewPager.
Fragments in the ViewPager are fixed, so should there contents will not go till the application manager wants to destroy it.Instead of trying to replace the fragments in the adapter, try to give a different set of fragments and notifyDataSet changed, or take the advantage of FrameLayout to show another fragment over the view pager tab's current fragment.
There is my solution that works:
Swipe Gesture applied at Fragment level along with ViewPager with it's default swipe disabled

Viewpager pass bundles between fragments

I have a custom Viewpager in which it disables the touch events. So I have buttons 'Next' and 'Back' that control the viewpager. My question is how to pass data or bundles between the fragments in the viewpager. Normally it would work but sense the fragments are created even though they are not shown. That's because of the viewpager slide effect, it has to make the fragments ahead and before for the effect to work. So that means I can't use bundles since the fragment is already created. This is what I'm trying to do
Fragment 1 -> Fragment 2 -> Fragment 3
Fragment 1 is created and so is Fragment 2. When I press 'Next' Fragment 2 is shown. I want to pass a bundle to Fragment 3 when I press 'Next' again but Fragment 3 is already created so it won't work.
Another way I thought of is to call a method in each Fragment when the Viewpager sets it as its current Item.
Why don't you create an interface that all of your fragments implement? This interface will have two methods: getParameterData() and setParameterData(). In your ViewPager, when they press next or previous, call getParameterData() on the current fragment, then call setParameterData() on the fragment to be displayed.
You could share/pass your objects between fragments by holding them in the host activity.

Categories

Resources