Calling Fragment Activity from a fragment - android

I'm initializing three fragments in my FragmentActivity that are actually tabs (TabHost and ViewPager), among those one is a list fragment.
Next I have another fragment activity with three tabs(fragments), Now what I want is on click of the list item of the list fragment, to call another fragment of 2nd fragment activity (which means that now the tabs that should come up are from another fragment activity ), also I want to pass some value from the selected list item to that next fragment. How can I achieve this?

Related

ViewPager with two fragments containing set of cards

I am having an activity with a viewPager which holds two fragments. For example, let's name the two fragments as Fragment A and Fragment B. Each of the fragments are having a set of cards. If suppose, I select a set of cards from fragment A and swipe to fragment B, the selected cards still remain selected when I swipe back to fragment A. How do I deselect them on swiping to the fragment B?
Use replace() method while doing fragment transaction. It will remove all the previous fragment from the containerView and add the new one.

FragmentManager with muliple fragment and setting ActionBar on fragment does not work

I have an app with one activity and 4 main fragment, I added this 4 main fragment to the FragmentManager on onCreate of Activity and hide all for startup of app, when a tab is selected by user I hide the previous fragment and show the selected one, I do this for using the backstack of ChildFragmentManager of this 4 fragments, Because I want separate backstack for each of this fragments. After rotation I want to call setDisplayHomeAsUpEnabled for adding this button to current fragment if this fragment has nested fragment, but there isn't any way to find what fragment is in showing state and if I call this method in onCreate of fragment it called 4 times, and just the last fragment set the button(Not the current fragment)!

Fragment instance in fragment manger is not removing even that fragment got popped up

I have a Fragment with list view. OnItem click of that list view I will replace the fragment's container with the new fragment. The new fragment will have the view pager and fragment state pager adapter. This adapter will create three fragments once. Now, all the five fragments (List fragment, fragment which has the view pager and the three fragments which are created by the fragment state pager adapter) are in the fragment manager. If I came back to the fragment which has list view. All the fragments except the one with list view should be removed from the fragment manager. But, they are not removing.
The problem is, If I click an item in the list view It will create three fragments all the time. And If I do orientation change, the fragment manager is recreating all the fragments. Each time I do orientation change three fragments are getting added to the fragment manager.
Is there any way to remove the fragment associated with view pager when I come back from the view pager fragment.
Without seeing the code I think this is your problem:
Every time your orientation changes, you add a new fragment manager transaction to the backstack. When you pop the backstack, you don't go back to the initial state but to another state where you still have the same fragment visible.
There are multiple ways around it. Choose one:
Don't add the fragment manager transaction to the backstack when you replace the fragments. When you need to go back to the initial fragment, just replace the contents with the fragment that you want. You can always keep a reference to the fragment in the activity if you don't want to re-create the fragment.
Pop the backstack before replacing the fragments and adding another transaction to the backstack. This way you are not accumulating transactions in the backstack. When you pop, the backstack you will always come to the previous state.
Name your fragment transaction so you can pop the backstack with popBackstack("fragmentTransactionName", 0) or popBackStack(transactionId, 0)
See http://developer.android.com/reference/android/app/FragmentManager.html
Hope this helps

Should a ViewPager that creates ListFragments have each ListFragment call back to the ViewPager to handle list item clicks?

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?

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