I use the viewpager and fragment, the adapter is FragmentStatePagerAdapter, fragment's layout i use is SwipeRefreshLayout(22.0.0)+ListView, the ViewPager's setOffScreenPageLimit is 1, Just think i click in all the fragment, current is fragment 1 tap the refresh then click to fragment 3 then go back! the ghost is come up!
Related
I have a FrameLayout container in which I use the replace method to put a fragment when an item is selected from the navigation drawer.
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment, fragment.getFragmentTag()).commit();
I have a fragment which has a ViewPager within it. I have 3 fragments which are displayed in a tab layout with the ViewPager. When I select another item from the navigation drawer the fragment with the ViewPager is replaced but the fragments within are not detached. The system calls only the fragment with the ViewPager onDetach/onDestroy etc. methods but not the methods of the fragments it contains.
I am using FragmentPagerAdapter and I tried with the FragmentStatePager adapter. Nothing changes. I am using the default setOffscreenPageLimit(1);
How can I get my ViewPager fragments methods onDetach/onDestroy called?
Thanks!
Are you using the FragmentManager or the child Fragment Manager? If you create a Fragment inside another Fragment and you want the inner Fragment to behave like the outer, you should get the child fragment manager from the outer fragment and use it to create the inner fragments. See this.
I have a single activity that uses a bunch of different Fragments. I have a TabContainer Fragment that holds a TabLayout which uses a ViewPager to handle tab navigation. Each Tab is its own Fragment.
In one of my tabs, I want to tap and place a fragment on top of my Tabbed fragment. This is meant to be a "details" sort of screen, so I don't want the tabs to be visible. I'm using this and it works as intended:
fragmentTransaction.replace(android.R.id.content, fragmentToDisplay).addToBackStack(null).commit();
Now, when I navigate back, the content in my tab is empty. The content in the tab directly next to that tab is also empty. When I navigate two tabs away, the content is recreated and the normal functionality returns. Why is content not being recreated on the tabs initially when I remove my "detail" fragment?
It turns out that I was simply not passing the correct FragmentManager to the FragmentStatePagerAdapter.
I needed to call getChildFragmentManager() on the Fragment, not getSupportFragmentManager() on the activity.
Thanks to these two posts for the answer:
Fragment in ViewPager not restored after popBackStack
and: Replacing ViewPager with Fragment - Then Navigating Back
I asked this question before getChildFragmentManager() and viewpager without any responses, probably because the explanation was too long.
Simply put I have a viewpager inside a fragment. The pages inside that viewpager need to be replaced through a series of screens. Pressing back on those screens will not exit the application. How do I do this?
challenges:
viewpager xml does not use framelayout. when I use the replace method, what container
id do I use?
when replacing the fragments why would I use getChildFragmentManager each time if that is private to the current fragment?
#goonedroid: that's not the case I'm looking for here. The only similarity is that the fragment has a viewpager. My viewpager's pages need to be replaced when clicked:
I have a navdrawer. Clicking item 2 shows FragmentA. Clicking FragmentA replaces it with FragmentB. Clicking FragmentB replaces it with FragmentC. Clicking FragmentC takes you back to FragmentB. Fragments are added to the backstack for proper back navigation.
Clicking item 1 in the navdrawer shows FragmentZ with a viewpager. The viewpagers pages are just FragmentA and all the aforementioned behavior. But going from A to B here, then pressing back shows no pages in the viewpager.
I finally figured out a solution, not sure why this works because I thought an Activity's getSupportFragmentManager() returns the same fragment manager as its fragment's getFragmentManager().
Anyway, instead of getFragmentManager() in the fragments, use getActivity().getFragmentManager()
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
I have a view pager inside a fragment. Inside the inner fragment i have to show animation when the fragment is visible. i checked this answer and tried setUserVisibleHint and OnPageChangeListener both this are called before onCreateView and gives null pointer exception. how can i find when Fragment becomes visible in ViewPager so that i can start animation.
#bill gates, viewpager is the thing, in that both page (Current page and next or previous page) both are prepare simultaneously itself, so when you slide the current page two pages are visible to user. that a awesome function of viewpager. thanks.