setOffscreenPageLimit doesn't load next pages, only keeps previous ones - android

That's the behaviour I'm getting on a Galaxy S3, sdk version 17.
Previously visited pages do stay loaded according to the parameter passed to setOffscreenPageLimit but the next page will only load after I start scrolling towards it, regardless of the parameter.
How can I make it pre-load next pages?

It doesn't matter what parameter you set this attribute on viewPager, it should still load next page. But it may not REload that page, if it is in given size. For example if you say setOffScreenPageLimit(3) and if you have 3 pages, then all 3 of them will be loaded only once and then they will never get loads again. (Unless you manuelly refresh them)

If you are using a fragment adapter (and even if you aren't) there's nothing stopping you from instantiating any number of fragments that you'd like to do the preload that you'd like and store them in your adapter.

Related

Viewpager not clearing fragments

I am having this problem with nested viewpagers.
I have two levels of viewpager. Lets call it levelOneViewPager inside which I have one more viewpager, let's call this levelTwoViewPager.
The Activity loads the data for levelOneViewPager and sets the adapter.
The levelOneViewPager loads data for levelTwoViewPager in onViewCreated and sets the adapter.
Everything works fine when the levelOneViewPager loads data for first time.
Now here is the problem.
First page of levelOneViewPager is loaded, I move to 4th page and come back to first page and the app crashes.
Reason, now first page of levelOneViewPager is recreated and is loading the data, even before it sets adapter, the fragments of levelTwoViewPager are created and is accessing some data in the parent fragment which is not available yet.
How is this happening? I haven't set the adapter to levelTwoViewPager yet but fragments are created for it.
(I tried using both FragmentPagerAdapter and FragmentStatePagerAdapter, it's the same behaviour)
add offscreenpage limit to parent viewpager this should fix the issue

screenshot of all Views in ViewPager

I have a ViewPager that consist of List of ScrollViews. I want to make a screenshot from all of the Scrollviews after the first load of the ViewPager.
After ViewPager is created, i am going through the Views and creating bitmaps, but i get a screenshot only for the first pages that are loaded offscreen.
If i use the command:
pager.setOffscreenPageLimit(totalPages);
than I am able to get all of the screenshots. However this approach is memory consuming and my app crashes with outOfMemory when the ViewPager consists of a lot of pages.
Is there a way to create screenshot of all the views even the ones that are not preloaded - offScreen?
If you don't set offScreenPageLimit(totalPages), ViewPager will initially only load 1 more page other than you see. That means other pages will not be even created until you swipe there. That also means you cannot take screenshot of a screen/layout that is not created/inflated yet. And yes, it is not a good practice memorywise to set offscreen page limit to a number that ViewPager loads all pages at start.
Answer: No, if you mind your memory consumption. Otherwise, yes.

Avoiding the default loading of fragments in a view pager

i am new to android. i noticed that on implementing the adapter of the view pager the getItem() will result in returning two adjacent fragments. How can i skip this default loading and load only the fragment in the current page. Please help me.
You should not do that. Please note that when swiping to next page two fragments are visible at the same time so they have both be created.
You can use ViewPager.setOffscreenPageLimit(1) for this. I've used that to keep all my three fragments alive. I Don't know what happens if you set it to 1.
Confusing, this is from the documentation. It says this value defaults to 1 but when I was working on that I got always two fragments like you do:
You should keep this limit low, especially if your pages have complex
layouts. This setting defaults to 1.

Is ViewPager really what I need for this scenario?

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.

Prevent WebView reloads in FragmentPagerAdapter?

I have a FragmentPagerAdapter used to show about 6 tabs, all of which load their data from a web server. One of the tabs contains a WebView that loads an image from my server. The server side costs of generating the image are high, and thus I want to reduce the number of calls to reload the WebView. For the non-WebView tabs, I have been able to save my state (for those, just a simple array) and restore them as tabs get swiped through.
Problem:
WebView reloads every time I swipe back to it using FragmentPagerAdapter, leading to high reload times and high load on my web server.
Solutions Considered:
Use ViewPager.setOffscreenPageLimit()
This is problematic because it will force more tabs to be loaded, even if they are never going to be viewed. This is needlessly expensive on my server.
Use WebView.saveState() and WebView.restoreState()
The documentation has been updated to make it clear display state is no longer maintained here, so this is no longer useful for this scenario.
Set my activity to have: android:configChanges="keyboardHidden|orientation|screenSize"
This works for the rotation case, but doesn't affect the ViewPager/FragmentPagerAdapter swiping through tabs case.
It sounds like the old behavior of WebView.saveState() would have been perfect...
The problem appears to be that you are wiping out your own results.
While you need a new WebView on a configuration change, you do not need a new WebView otherwise. And, if you already have the WebView, you do not have to tell it to do anything.
So, I'd try this:
Hold onto the WebView that you create in onCreateView() in a data member of the fragment
Only inflate the layout in onCreateView() if that data member is null
Only call loadUrl() if you inflated the layout
(if you are creating the WebView in Java code, replace "inflate the layout" with "create the WebView via its constructor")
If the contents of the fragment is more than the WebView, you will also need to hold onto the root view that you inflated in a data member, so you can return that from onCreateView().

Categories

Resources