Why do FragmentPagerAdapter create 2 fragments at the same time? - android

I used the wizard to create an tabbed Activity which contains an FragmentPagerAdapter, in the getItem() function I tried to return my own fragments as follows:
However when the App runs and the view is in the first tab, I found it actually goes through the onCreateView() function in both Fragment and Fragment2(known from println() in onCreate() & onCreateView()), and when I swipe to Fragment2, it prints Fragment3. and when swipe to Fragment3, it prints nothing.. and after that I swipe back to Fragment2, It prints Fragment1.
What's wrong with it?

Thats is how a FragmentPagerAdapter is supposed to work. It created the Fragment in selected position and also the previous position and next position. This is so that when you move from one fragment to another through swipe the next fragment is ready so that it can be animated into the view. You can read about it below.
http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html

Related

Fragment initialized without onCreate being called

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.

I am not able to find callback in first two fragments of viewpager in activity

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 .

Viewpager inside fragment and back button

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()

How to replace Fragment inside ViewPager, using PagerAdapter?

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 )

OncreateView Issue

I have an application with a fragments. In one activity there is 3 fragment which is frag1,frag2 and frag3 . It is possible that The OncreateView will trigger is just for frag1 only . In my case frag1 and frag2 are the first to read the onCreateView Event. I just want the frag1 only and when you swipe the frag2 will read and so on.
With the viewPager you are able to set the number of fragment created with the method setOffScreenPageLimit but unfortunatly I think you will not be able to create only one fragment (the one visible). What will happen when you will slide? As the second fragment is not created, a blank screen...
To be smoothy when scrolling, the second fragment view has to be already created.
Check this response: ViewPager.setOffscreenPageLimit(0) doesn't work as expected

Categories

Resources