I have this structure
Tabs with fragments
---(one)Fragment with ViewPager2
------- Fragments inside ViewPager2
When I change between ViewPager's fragments, onResume and onPause are called just fine.
Problem comes when I click on another tab to load another fragment. When I do that for some reason the fragment that hosts the ViewPager will receive onHiddenChanged callback but its children will not receive any callback.
I've tried all 3 constructors for FragmentStateAdapter, none of them helped and I have no idea how I will notify viewpager's fragments for such a lifecycle change. Any ideas?
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 am using FragmentTabHost to display the Tab inside the fragment. From parent fragment I am calling a child fragment inside the child fragment I want to hide the tab please any one help me on this
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.
My problem
I am using a ViewPager to display fragments inside a FragmentActivity. ViewPager gets fragments from the attached FragmentPagerAdapter.
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mAdapter = new HomePagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mAdapter);
Suppose ViewPager has 3 fragments to display: Fragment1, Fragment2, Fragment3. Fragment1 is a grid fragment and displays a grid. Fragment2 and Fragment3 have their own content to display.
When the use swipes on the screen, ViewPager will display the next fragment - Fragment2. And so on.
What I want?
What I want is that, when an item from the grid (displayed by Fragment1) is clicked, Fragment1 should be completely replaced with some other fragment, say Fragment4 (this is different fragment and is not returned by the attached adapter). User will work on Fragment4 and after ButtonBack click (just button iside Fragment4), Fragment1 with the grid should be displayed again. Meanwhile ViewPager should behave the same i.e on swipe, the next fragment (in our case Fragment2) will be displayed.
So I just want to get the same behavior as in example:
http://developer.android.com/training/basics/fragments/fragment-ui.html#Replace
My question
So, is this possible to achieve? If yes, then how to?
I would greatly appreciate for your help. Alex.
P.S. Sorry for my English:)
I've made a little example that shows how to achieve it:
https://github.com/danilao/fragments-viewpager-example
I think the point is to use another fragment as a container.
One way to achieve this is by adding another FragmentActivity that displays your Fragment4. Use Intent to start this activity and sent grid item position or other data in Extra.
On press of backbutton this activity will be finished and last activity with view pager will be displayed.
[EDIT] Old response. Better solutions are provided in other answers,
You will have to do the fragment transaction and add the existing fragment into backstack.
I created a sample project to show you how to do it.
The code is not fine tuned, I just created it to show you how to do it.
https://drive.google.com/folderview?id=0BxHClVwHSqq5dVRvT1Qyd0hYN0k&usp=sharing
But please be aware that even if you press back at any of the view pager screen, the previous fragment ends up showing as it is the same activity.
IF this is not expected behavior, then you should consider having multiple activities instead ( like tab layout )