on the page:
onCreate called for two tabs each time one tab is selected
there is explained how the ABS(actually ViewPager) is working in order for ViewPager to be able to do a scrolling.
It is clear that at least a prev/next page need all to be created at the same time.
Would it be possible to "cache" more than just prev/next Views(Fragments), in a way:
I am on Page 1 and there I have a network call to fetch some data(doing this in Activity, not in Fragment - btw. is this OK?)
switch to Page 2, and then
switch to Page 3, and then
switch to Page 1 = Here my page is recreated (using some caching though, BUT, I do not need any recreation if possible)
So, it would be nice to cache all the pages. How to accomplish this If possible in current version (4), or this would be some new feature?
Or even better question, how to postpond/disable destroying of views?
I suppose It might be useful to someone else, as pointed on website
http://storkme.org/2011/12/tabs-done-right-viewpager-and-fragments/
There is already a method which allows you to predefine number of "cached" pages the ViewPager will save from recreating.
mViewPager.setOffscreenPageLimit(3);
Thanks Jake for reminding me to do a different G search :)
Related
I have a viewpager, it has 5 fragments page. I can switch between each fragment by tapping a navigation button.
Now, what I'm trying to achieve is to refresh 1 of the page when entering it. (e.g. I'm in Page 1, when I tap button for Page 2, Page 2 will display and refresh.
However, I find out that the onResume method in the fragment is not called on this behavior. or any of the other lifecycle method.
I can't seem to find a way to do this. Is there any callbacks I can use for such scenario?
Since you don't need any kind of caching, it's probably a good idea for you to use viewPager.setOffscreenPageLimit(0) so pages won't be loaded in background, and onResume will be called only when page is actually displayed.
UPD: Thanks, Yarh, for pointing out that setOffscreenPageLimit(0) doesn't work. Sorry for that.
If this doesn't work for you, use viewPager.addOnPageChangeListener(listener) - onPageSelected(position) method should help.
use onTabSelected and onTabReSelected methods of navigation tabs to do your stuff
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 :(
I have an scrollable tabs + swipe activity with 3 different fragments on it. It works well but I don't want my fragments to be destroyed when I navigate between them. For example, when I create the activity, it's shown the first fragment but it's created the second one too. When I swipe to the second fragment, the third is created, so I can swipe to the third one and it will be created before I open it. When I return to the second one, my third fragment still stays created but when I return to the first one, the third fragment is deleted.
There would be any way to avoid this? I don't want my fragments to be deleted when I navigate between them.
If you use a ViewPager, it has a method called setOffscreenPageLimit(int limit). Try and see if setting that higher works
This answer should help you figure out what's going on. Pretty much you are most likely using a FragmentStatePagerAdapter, when you should be using a FragmentPagerAdapter. That keeps it from deleting fragments not in view.
If, for performance reasons, you need to have the fragments deleted when they aren't being used, there are other options. Either look at why the performance is that bad when you have only 3 tabs (It shouldn't be unless you are doing something wrong with a listview, or have a map open, etc.) or you can code the fragments to save their state when they are being destroyed (which should be happening anyways with a FragmentStatePagerAdapter).
If that doesn't help, we'll need more info on what you are doing exactly to help.
APPLICATION IDEA: Idea is simple. When application is launched it will connect to web service which will return number of rows in particular column in database, then it will retrive data from first row and display in activity. When user interacts with its finger, left or right (e.g swiping through pages), it needs to load next (or previous) row from database.
Please check following picture you may understand better.
PROBLEM: Since I'm new in Android I would like to know which scenario best fits my needs. I actually need to load data from database when user swipes and show this on my current screen.
WHAT I'VE TRIED: I already made my "homework" and I searched google for my problem. I used implementation of ViewPager class with PagerAdapter class. But when I swipe on the next page nothing is showed ( Yes I used AsynTask and I sucessfully pulled data to my device), but instead is showed on the NEXT page.
So I need to know if ViewPager is really what I need? Do I need to use ViewPager together with FragmentPagerAdapter so that my text will load exactly when my page is showed?
ViewPager suits this scenario well.
Note that ViewPager by default loads the next and previous pages while the current page is being shown (off-screen page limit 1). Your adapter likely has some bugs that make it pull data for the wrong page. To get help with them, please post a more specific question.
I'm using FragmentPagerAdapter with a tabbed interface. My interface happens to have three tabs. I can see that as soon as my app is created, the adapter requests that both tabs one and two both be created immediately (I assume this is so swiping to the next tab appears smooth to the user).
Is there a way to stop the automatic loading of the next tab? My tabs are pretty heavy, and loading two right at startup is taking a good deal of time,
Thanks
Is there a way to stop the automatic loading of the next tab?
Sorry, no. This is driven by setOffscreenPageLimit(), and the minimum value is 1, meaning that ViewPager will always try to create and hold onto at least 1 page on each side.
It sounds like you need to move more logic into background threads.