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.
Related
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.
Scenario: I have three fragments in ViewPager in activity.
Fragment One
FragmentTwo
FragmentThree
I noticed that switching from fragment one to three onPause() method is called
but when I switched from fragment one to two onPause is not called
Problem: I want to show some data in FragmentOne's TextView with a webservice on button click.I clicked that button and data is shown But I noticed when I switch from one to two and then come back to fragment one data is shown. I want to hide that data when I come back to one. Is there any callback . ?
You need to understand Viewpager first. viewpager creates fragment in advance before and keeps so that there is smooth flow when you switch, like if you are on Fragment 2. viewpager creates 1 and 3 and keeps.
and when you are on Fragment 3 it retains Fragment 1.
Anyways you can increase the limit of viewpager with this
viewPager.setOffscreenPageLimit(int num)
you can understand all the lifecylce of fragment in Viewpager by putting Logs in onPause and onResume in viewpager fragments.
Anyways coming back to your prob. you need to have custom interface like this . answer helped me too
Its happening because Fragment One is in backstack. Dont keep FragmentOne in backstack while switching to FragmentTwo and on Back press of FrgamentTwo call switchContent for FragmentOne again .
I have 2 viewpagers containing different fragments now I have to move to the fragment of one viewpager to fragment of another viewpager. But when I hit back the previous fragments data have to hold. The problem here is I need infinite loop navigation i.e., from fragment1 of Viewpager1 contains a textview when I click on that it has to go to details screen which is in fragment1 of viewpager2 and another textview which is in fragment1 of viewpager2 it has to go to fragment1 of viewpager1.
I have tried using stack and I can navigate but when I go to 3 or 4 levels and hit back I have lost the previous data so getting nullpointer exception.
Can anybody help me?
The way I accomplish similar setup is to use an array list to hold the data and pass it around from fragment to fragment using Intent.
Alternatively create a class on the host Activity with public access method so it can be get and set from the Fragment or better still implement a callback that updates the class on the Activity from the Fragment.
This way you can have a text view for firstname input in one fragment and each time and when a text is inputed in that text view you will update the Class.Firstname property in the Activity and the when you go to the details Fragment you can get the Class.Firstname and display it.
Thanks
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.
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