I'm having a bit of trouble with communication between a parentActivity and its child fragment.
I am using actionbarsherlock.
I have looked around on SO, but I haven't found something that deals with ViewPagers and Spinners yet.
Background:
My child fragment has its own action bar specifying a couple of extra action items not available in other fragments, one of these action items is a spinner with multiple options - A, B and C etc
By default the MainActivity will load Fragment_A(child fragment) which contains a ViewPager.
This view pager handles swipes between views A and B(Spinner has options A,B, C etc)
Fragment_A also has spinner adapter. If you select A on the spinner, the viewPager switches to A, if you select B on the spinner, the viewPager switches to B. (This works fine).
My question is: How do I capture when the user "swipes" between A and B(They don't use the spinner) and change my "spinner" to say A or B respectively?
Note: My pager adapter(for ViewPager) is created separately in a different class.
Any help is appreciated...Thank you all!
This is what you need. You'll have to set up an interface in your Fragment and implement that interface in the parent activity. In your Fragment instantiate an instance of the interface and in whatever method you use to listen to page changes, call the method to communicate that data to the activity.
It seems kinda "hand-wavy" but I hope that clears it up a little.
Related
Description
I have a Fragment which has a ViewPager and BottomAppBar. ViewPager is showing 3 fragments, each fragment has it's own FloatingActionButton (as I am using Navigation Component, in my case each fragment is a container of navigation graph).
The Problem
Each time the user is swiping the ViewPager I want to show the fragment and also I want to attach the FloatingActionButton to the BottomAppBar. The problem is that Fragments does not know anything about BottomAppBar as they are in separate modules. What is the best way to do this?
This answer is in 2 parts and there are a number of ways of doing it.
First you need to know when each Fragment is shown to then trigger the Fab to change.
This can be done via 2 method, either use the changes in the Fragment's lifecycle to trigger this change or with a PageChangeListener.
With the current version of Viewpager you can use BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT https://developer.android.com/reference/androidx/fragment/app/FragmentStatePagerAdapter#FragmentStatePagerAdapter(androidx.fragment.app.FragmentManager,%20int)
The onResume method of the Fragment is called when it becomes the primary item (the one shown on screen), this can then be used to trigger the Fab change.
The alternate is to add a PageChangeListener and onPageSelected(int) method https://developer.android.com/reference/androidx/viewpager/widget/ViewPager#addOnPageChangeListener(androidx.viewpager.widget.ViewPager.OnPageChangeListener)
The second part, depending which option chosen and the structure of the project you will probably need to communicate that change between components/fragments.
This can be done a number of ways, most common are interfaces or shared viewmodel.
More details of inter Fragment(component) at https://androidwave.com/passing-data-between-fragments/
e.g.
The activity or possibly the BottomAppBar implements an interface the each Fragment can call to say it is the primary Fragment
Or the BottomAppBar observes the shared viewmodel value that defines which Fragment in on screen and each Fragment updates the share viewmodel value with it's number when it is the primary Fragment,
It's more like a conceptual question. Actually i have three fragments let's say A,B and C all are hosted in same activity. Fragment A and B are having List. In both fragments , on click of list item i am loading Fragment C. Now how to identify whether Fragment C is loaded from Fragment A or Fragment B ? I have to make changes in Fragment C's UI when it is loaded on click of any list item in Fragment A.
You would need to detect clicks in Fragment A and B. In their OnClickListeners, you could create two different setup methods for Fragment C.
on click of list item i am loading Fragment C
Since it's you who handles the click, simply add whatever you want to make other elements recognize the source of the click. Yet, I'd still say this is wrong approach - you should not really care what fragment it was literlay but rather, assuming fragments A, B reflect different data types, what data type it was. But still - you need to add that data to your bundle (or whatever you use to pass the data to fragment C)
I have an activity with a ViewPager as part of its layout that creates a series of ListFragments as its pages. When the user clicks on a list item in one of those ListFragments, it takes them to a detail page that allows them to edit the details of that list item.
So in the ListFragment's onListItemClick() method, I first check to see if it is in tablet (two-pane) mode, and if it is, I perform a fragment transaction that replaces the right-hand detail fragment with the detail fragment for the newly clicked list item. If it is running on a normal phone, I just create a new activity that takes up the entire screen. So basically, I currently have the ListFragment perform the new fragment/activity creation for when the user clicks on one of its items.
But instead, should the ListFragment's onListItemClick() method call back to the ViewPager activity, and have the ViewPager activity contain this fragment/activity creation code?
The problem is that a ViewPager activity can only create fragments, so I cannot see how I can have each ListFragment call back to a custom ListFragment parent activity. As far as I can tell, the ListFragment must have the ViewPager activity as its parent activity. So if this is the case, is it good practice for the ListFragment to call back to its ViewPager parent activity to handle the fragment/activity creation for its list item clicks?
Hi I am developing android application in which I am using one activity and two fragments. Consider same example which google explain like one list view and detail view. on click of list item we are rendering respective detail fragment.
So I learn how to do fragment transaction and i come up with two solutions. One which is standard way which google explain that make one interface and implement that interface into main activity. And do fragment transaction there inside main activity.
I tried with another way. when I click on list item inside click listener instead of calling interface I change fragment inside my list fragment only and its working fine.
So i want to know what is difference between those to methods. changing fragment from main activity and changing it from fragment only.
What kind of problem i will face if i implement with second method.i.e. changing from fragment only.
Need Help. Thank you.
What kind of problem i will face if i implement with second
method.i.e. changing from fragment only.
There isn't an actual problem, it's more of a design discussion. Using the second approach means you're making a very specific fragment, one that on a click on one of its rows will make a transaction with a specific fragment on a specific place of the holder activity. This would be a problem if you plan on reusing this fragment.
Suppose you have this ListFragment and you decided that it should be used in five other activities(with different data). Because it has a very precise action when clicking one of its rows, this fragment will always require the holder activity to have a specific container(where the transaction will be done) along the specific detail fragment with which it was initially used. The problem is that in each of those five activities you may want to use a different fragment when clicking a row of the ListFragment, this would require making changes to the class of the ListFragment.
Now suppose you have the same behavior with the interface approach. As the ListFragment doesn't know or care who handles that click event(as it passes it to whoever registers as the listener) you can simply put the ListFragment in the five activities with no problem(so no changes to it at all). In the interface method of the activity you could then implement the desired behavior with whatever fragment you want and in whatever container setup you want.
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.