Android ViewPager with FragmentPagerAdapter Issue - android

This is a repeatation of a post Recreating off screen pages of ViewPager while changing orientation as i dont get any response and i am in a big trouble.Moreover i am repeating the whole text here.
Problem:
From internet i came to know that Android ViewPager needs to set at least 1 offscreen page limit for both side with viewPager.setOffscreenPageLimit(1), in that case when i am watching a page, two more pages in both side of the middle page have already been created. Now the problem is when i am going to Landscape mode and swiping some page and then turn it back to the Portrait mode, only the middle page is being recreated (not the offscreen pages). May be that is why a shadow/marginal portion of the previous page (that was created with landscape margins) is always shown overlapped with the middle page (that is recreated with portrait margins).
Now how to get ride of this problem. I don't want to see this overlapped portion of the previous page. I have attached an image just after going from Landscape mode to Portrait mode.

You can try instantiate adapter with getChildFragmentManager() instead of getFragmentManager() or getSupprtFragmentManager(). I had a similar problem and all I did was it...

Please use getChildFragmentManager() when using viewpager inside Fragments and getSupportManager() when using pager in activities as source for pager adapter

Related

How to show fragment with data again when view is swiped back to in Viewpager having a tab layout?

I am trying to read RSS feed and show the content of it in card view which is inside ViewPager with tab layout. It shows fragment with data initially, but when swiped back from another tab, the whole card layout with data,disappears.
I had to handle the same situation. I had TabLayout in ViewPager.
I have used FragmentPagerAdapter with off screen page limit set to (NO_OF_TABS - 1). This is to hold all the fragments in memory and they wont be destroyed and recreated as you swipe back and forth. As my fragments are small in size meaning not many views every fragment has so it was okay for me. But this way fragments always holds the latest data. Say, if user had entered text in EditText and swipe to end and come back, then fragment will still have the text that user had entered.
In your case, if you have not set the page limit the default is 1 so the fragment gets destroyed when you swipe away so you lose the data.
IN ADDITION TO ABOVE:
If you can not keep all fragments in memory, you may consider using FragmentStatePagerAdapter. This adapter saves the states of fragments that are being destroyed and it will use the same state to represent in fragment when user swipe back to this fragment. But there are issues around StaePagerAdapter while it destroys adn recreate the fragments, there are indexing issues so you will easily get IndexOutOfBoundException if you swipe. This is with page screen limit set to less than the total fragments.

Current item in Viewpager changes with different orientation

I have a fragment, which consists of a viewPager in the top of the screen and two buttons at the bottom.
The buttons are for an additional navigation besides the swiping through the pages.
The viewPager consists of about 10 pages with some input fields.
However, when I swipe through the pages and rotate my phone to landscape, the viewPager automatically changes the current item and selects the first page again. Another effect is, that all input data to this point are cleared.
This is really annoying because you have to start all over again.
Is it possible to switch between landscape and portrait mode without this issues?
Some background information:
the app consists of one activity, which is filled with different fragments
a navigation drawer is implemented to choose which fragment will be shown
When you change orientation you are essentially refreshing your activity losing your data such as current page, fragment etc, you need to save your data across the states, i would suggest looking into savedInstanceState.

ViewPager refreshing itself

Am running in some weird problem. I have implemented ViewPagerTabStrip by following the tutorial Here
It's working fine and i have implemented Three Tabs.
On the first Page /tab , i am loading the data from server & displaying it in the ListView
The problem is, when i swipe to the Third Page & then i swipe back to Second page, the data of the first page is being loaded from the server again.( View of first tab is re-created.)
From the logs, it seems that this is the behavior of ViewPager & Adapter.
When we are on the first tab, it creates the view of second tab/ page as well in background & when we are on the Second tab, it was doing same for Third tab in background & upon reaching to third tab, it didn't created the view for it self(i.e third tab).
But when we swipe back to second tab, it re created the view of first tab as well.
How to solve this ? we can't afford to load the data from server again and again while swiping through the tabs.
I mean is there any way to stop the reload of data (or re-creation of view) while just swiping through tab pages? It should load the data when the whole fragment activity of View Pager is created and not at the time of swiping.
Any help on this please ?
Yeah, the ViewPager only keeps a certain number of fragments in memory before destroying them- this default number is usually two. Thus, what you have to do is tell the ViewPager to retain those fragments.
The simplest solution is to simply tell the ViewPager to keep three fragments in memory as follows:
viewPager.setOffscreenPageLimit(3);
Here are a few suggested answers on how to retain fragments (with more than just this method) and the reasoning behind the default behaviour as well as these solutions:
ViewPager and fragments — what's the right way to store fragment's state?
retain state of viewpager fragments android

Android ViewPager Gallery

I am using a view pager to show a list of images, it works fine but the main issue is that when I swipe between images, there is some kind of fading between images.
for example if I have 5 pictures and the first one is visible when i swipe the second one is visible directly.
if i decided to swipe faster, it shows white screen then the image appears "moving from first to third quickly - as example".
I think the fragment is being recycled - is there any way to avoid this ??
It's hard to tell with no details on your implementation, but just to answer your last question (I'm not sure if it will necessarily solve your problem) - if you are using a fragments in your ViewPager and are currently using a FragmentStatePagerAdapter, you can switch to a FragmentPagerAdapter, which will keep the fragments in memory (although it will of course use more memory, so you might not want to do it if you have a large number of fragments in your ViewPager). You can also specify how many offscreen pages for the ViewPager to keep using
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(2);
In the above case it would keep 2 pages on each side of the currently viewed page. Sorry, I would have confirmed the specific problem with you before posting this answer, but I don't have the status yet :(

Restoring a fragment using FragmentPagerAdapter

I have an application using two tabs, both containing fragments. I am using the FragmentPagerAdapter to manage tab changes. All works fine with two tabs.
I recently added a third tab and am having a bit of trouble. tab1(fragment1) is a LinearLayout of small fragments. tab2(fragment2) is a simple layout of text. tab3(fragment3) is also a simple layout of text.
If I switch between tab1 and tab2 both work properly and retain their states. However, if I switch between tab1 and tab3, tab3 shows the text properly but tab1 shows a blank tab.
I know that if a tab goes more than 1 position off the current position the fragment will be destroyed and the fragment will need to be recreated. Does FragmentPagerAdapter not do this automatically?
I discovered that if I rotate the device (with tab1 selected), the tab1 fragment will be restored with its correct state so the fragment is not being destroyed. It seems like there is an issue with the layout not being correctly recreated by the ViewPager but this is only a guess.
As a work around I set myViewPager.setOffscreenPageLimit(2) and the layout is retained. I would like to get this to work as I think its supposed to without forcing the fragments to stay in memory.
Reading docs for setOffScreenPageLimit(), makes me think your approach is fine. Look at this:
If you have a small number of pages (3-4) that you can keep active all
at once, less time will be spent in layout for newly created view
subtrees as the user pages back and forth.
You should keep this limit low, especially if your pages have complex
layouts.
I'm curious though if setRetainInstance(true) will work, since it's the Adapter who decides to recreate a fragment not that it's get recreated during some Android change config operation.
Add
setRetainInstance(true);
in onCreateView method of a Fragment

Categories

Resources