Saving tab information after swipe - android

I have 3 tabs in my application.
In my first tab I need to read JSON data from URL and using the user location. It takes about 2-3 seconds to load the page.
My problem starts when I implemented tabs, and now every time im swiping next and back, the info is re-fetched.
I tried the following solution:
I moved my method to the MainActivity, and created a variable named "isUpdated" and updated it to 1 if the info has been fetched successfully. since then, the tab data is gone after swiping and it shows blank empty tab.
I understand that the tab data is wiped after swiping to another tab, but I need to save the tab content.
How I can save the tab information and show it after the user swiped to the next tabs, without re-fetching the data over and over again?

if you are using ViewPager then set your off screen page limit to something different
for example
mViewPager.setOffscreenPageLimit(2);
should be enough for 3 tabs

Related

ViewPager show empty screen initially but displays data on swiping back

Android ViewPager shows empty screen initially but displays data on swiping back.
Scenario:
I am using 3 tabs with recyclerview. I am using volley library to get server data. On loading the app all tabs are empty but on swiping to the 3rd tab and back to the first the data displays on the 1st and 3rd tab. The Second tab remains empty.
Any solution is appreciated.

The fragment data of the corresponding tab of viewpager need to be loaded only when the tab is clicked

I am having a viewpager and having 3 tabs in it. All the three tabs are having their fragments. What I noticed that each fragment data is getting loaded at the first time when I am setting adapter to viewpager.
What I need is to load the fragment data of the corresponding tab only when the particular tab is getting clicked.
I have noticed one more issue in it. Like when the activity started in which I am having viewPager, then the data of the first tab's fragment get loaded at the same time the data of the second tab's fragment also get loaded and that is shown when I click on that tab.
Same way when I click on Second Tab, third Tab's Fragment date get loaded.
So what I need is that the data of the fragment should be loaded only when I click on the particular tab.
Thank you so much in advance..
You should use the ViewPager.setOffscreenPageLimit(0) to only load the currently displayed content.
Its the View-pager behaviour that it loads one page left/right to current page even if you have specified setOfflinePageLimit(0).
To load each fragment on click on tab, you should use LocalBroadcastReciever.

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-cache each fragment in viewpager but doesnt load until the fragment is visible to user?

I have a viewpager in my app with 4 tabs. I know I can do setOffscreenPageLimit(3) to retain all the pages from reloads. but my problem now is that all 4 tabs need to send request to server and that take some time, especially when all 4 requests are sent at the same time. Is there a way that I can do like setOffscreenPageLimit(0) which makes the app only loads the current page but retain the page when swipe happen?
Is there a way that I can do like setOffscreenPageLimit(0) which makes
the app only loads the current page but retain the page when swipe
happen?
No you can not, in order to have a better UX android dose not allow to set it to 0, so the default value and the lowest one is 1 which means it loads your off screen fragments of left and right of your current fragment.
what should you do?
3 different solutions I can offer you:
1) Send your request to server at the function onPageSelected(int position)
2) if you are using actionBar tab use onTabSelected and send your request to server.
3) send your request at the onResume of your fragment by using setUserVisibleHint and getUserVisibleHint function to determine when your fragment is really visible to your user so send it at the right time.

views in tab not getting displayed after database update

I am using two tabs in my android application.
In Tab A, the user enters some values and a database is updated based on the entered values.
In Tab B, I am trying to display the updated values of the database using TextViews.
But when I switch from Tab A to Tab B after entering the values in Tab A (i.e. after updating the database), the Tab B appears empty.
Only when I restart the app, the Tab B appears correctly with updated values.
Please suggest to me what could be the problem...
In Tab B's Activity you should put code that loads data from database and displays it, inside .onResume() method.
Check Activity Lifecycle document to see what lifecycle methods are run at what lifecycle stages.
Posting your code could be helpful as we can't tell from your post if you are using tab activities or tab view, but either way it sounds as if you aren't refreshing the data. You need to figure out the correct event and actually reload the text of the TextViews. When you are restarting your application, it is loading everything from the database, and so you see all the correct data, but when you update the data, all the widgets have already been populated, and there isn't anything that tells them that the database actually changed.

Categories

Resources