I have viewpager with 4 tabs, and using fragments in each tabs. And i have webview in each fragment. when I was in the first tab there are no mistakes, until the third tab. When i back to first tab, fragment refresh automatic. It make webview loadUrl() again. I don't want it. How to stop that refresh ?
Another explain:
I have 4 tabs. I call it, Tab AA, BB, CC, and DD with adjacent. When I in Tab AA and go to Tab BB, there are no mistakes. But when I in Tab AA go to CC and go back to tab AA, the fragment in tab AA refresh again. And i want to know explanation about lifecycle in viewpager ? Thanks..
According to http://developer.android.com/reference/android/support/v4/view/ViewPager.html
you can use setOffscreenPageLimit to set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.
Please take a look at the ViewPager Documentation. The default behaviour is to retain 1 page either side of what's currently displayed. You can change it using setOffscreenPageLimit (int limit)
just use
viewPager.setOffscreenPageLimit(4);
Related
I am making an application that has MainActivity that contains BottomNavigationView and FrameLayout above it. There are 3 Fragments say Fragment A, B, and C.
My doubt is, How do I make switching of Fragments as quick as YouTube Android application? By saying quick, I mean that, when I am on "Home" tab of Youtube application and I switch to the "Trending" tab and again go back to the "Home" tab, it simply loads "Home" tab within fraction of seconds, as if it just hided the inflated page in background and showed up when selected from BottomNavigationView. And also, It inflates the page exactly to the same position where I left.
When I am trying to implement the same in my Application, the RecyclerView in Fragment A re-inflates if I come back from Fragment B.
I am expecting the idea how they do it and in which method they do it (For eg. onStart or onDestroy or onViewCreated)...
If you are using viewpager then Increase the viewpager offset limit
viewpager.OffscreenPageLimit = 2;
Tt's limit is one by default. I hope this may fix your issue.
Thanks
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
I have got a weird issue with the 3 tabs of my ActionBar :
When i go from my second tab to the third, onCreateView() is not called, i like it because values in my third tab are in the same state.
Now, i will switch to the first tab and go back to the third, and now onCreateView() is called, and i don't want that...
Someone knows how to avoid this ? I don't understand why there is a difference between my second and my first tab !
Thank you for reading
It's probably because of the offscreenlimit in viewpager (I suppose it's your case).
It's set by default to one which means that view pager holds one fragment at each side of your currently visible tab in memory. (And when it's in memory it means that onCreateView() gets not called)
once again, I am following this tutorial: androidhive - tab layout with swipe able views . All 3 fragment tabs are loaded with different information and logic and layouts. I would like to refresh each fragment when their tabs have been clicked as it auto loads the fragments from start as it uses the view pager. Is there any simplest way out that I could load only a/1 fragment using view pager? Or to reload the fragments? Thanks for your help!
Try to add this to your viewpager:
MY_VIEWPAGER.setOffscreenPageLimit(0);
Check out the documentation.
Pages beyond this limit will be recreated from the adapter when needed.
The limit of 0 will cause a recreation of the fragment when you open a tab.
I have three horizontal tabs A, B and C. They have been implemented using FragmentPagerAdapter. Is there a way I can again implement horizontal swipe tabs in each tab.
Tab A
-- Tab 1
-- Tab 2
-- Tab 3
Tab B
-- Tab 1
-- Tab 1
-- Tab 1
and so on.
Do you mean you want nested tabs? If so, I would suggest looking into the documentation for FragmentStatePagerAdapter
Assuming you have fragments set up for each tab, embed a FragmentStatePagerAdapter within the existing FragmentPagerAdapter.
Copy and convert your activity to a Fragment and embed it in the existing activity.
WARNING!
This design is HIGHLY warned against by Google! I would not suggest using it.
Anyway, hope it helps.