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 )
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 a fragment that hosts a ViewPager with 3 fragments, 2 of them contain ListView (not inherits FragmentList)
The problem is:
If I click on one of the items in one of the list the application navigates me correctly to a new fragment, but, if I press the back button and go back to the ViewPager, a can see a blank fragment, only the first fragment who didn't have ListView is created, none of the other fragments are displayed, nor even their onStart is called
What's wrong? I suppose the fragment is not attached again, how can I solve this issue?
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 the following . I have a fragment on the left which is grid view of questions Q1,Q2 etc., If user clicks on Q1 on the right there is a fragment which shows the question details with 4 options. Now I want to implement view pager for the right hand side fragment.
My main looks like -
Fragment 1
Fragment 2
For the grid i have an activity which instantiates the question list and I have another fragment to show the questions.
Please can someone tell me how I can add a view pager only for the fragment on the right whilst using the existing logic .
You can add ViewPager to the layout of your Fragment2, either programmatically or using xml layout file.
If you wan't it to navigate through questions in Fragment1 and Fragment1 to react to it (by highlighting items) - you can broadcast an intent every time a page is flipped and handle it in Fragment1.
I'm not sure I understand what kind of problem you're having, can you provide more details?