Fragment Flow in View Pager in Android - android

I am using viewPager for tabs in my application . I have 2 Tabs and both tabs are making network call. As view pager inflate the both fragment in the starting so I created a interface and implemented in both fragment with a function onRefresh() which is basically doing the network call. Now issue which I am facing is, When I put my app in the background on 2nd tab and resume the application, it is calling the network api of first tab not the second tab.
I want the API of tab to be called which was visible when I put the app in the background.
Can any one tell me how can I achieve it?

Related

How to Prevent Android FragmentStatePagerAdapter from Creating Neighboring Fragments

I am creating an Android app, and I have linked aFragmentStatePagerAdapter to a TabBar inside one of my Activities to allow users to slide from Fragment to Fragment. Each Fragment in this Activity is populated by a REST call, and if it fails, a Dialog will pop-up saying you should try and refresh your information (only the first time you view the Fragment). The problem I am having is that since FragmentStatePagerAdapter creates the neighboring Fragments to the Fragment you are currently on, it is creating the Dialog(s) of said Fragments prematurely (For example if you are on Fragment 3, and Fragment 4 prompts a Dialog, it will show on Fragment 3 instead of Fragment 4). Is there a way to turn off the creation of neighboring Fragments using a FragmentStatePagerAdapter?
Thank You!
You can control the number of neighboring fragments that area created by calling setOffscreenPageLimit, but the minimum is 1.
Instead of creating your Dialogs in the fragments' onCreateView, I would do it setUserVisibleHint instead. That way the dialog is only created when you're certain that the user is on the page.
No i think you can't , Though there is a property for view pager
viewpager.setOffscreenLimit(0) //defaults to 1
but you can try some other solutions
show Dialog in onResume();
make an interface in Acitvity and fire up to it's implementation in Fragment
tab.setOntabselected{
Pageselected(pagenumber)
}

manage life cycle of fragments that use in SlidingTabLayout

in my app i use SlidingTabLayout to make tabs ,my app has three tabs for each tab attach with fragment hence i have three classes extend from Fragment when Run App
show me onCreateView and OnCreate of first and second fragment created together i want to when click on second tab onCreateView and OnCreate of second tab created
because i load things from webservice when click second tab
like facebook app that happen loading things when click on tab
Looks like you're using SlidingTabLayout combined with ViewPager. ViewPager by default manages 3 items (curent one on one on the "left"/"right" side) one of which is displayed and 2 others are in idle state. You can't decrease number of such items so the only way for you is to move your web service call from onCreate/onViewCreated to some other place. For example you may call setHasOptionsMenu(true) in your second fragment and implement your web service call in onOptionsMenuCreated callback of your second fragment.
There is a predefined method called setOffscreenPageLimit in view pager,
setOffscreenPageLimit(int limit)
Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.
For eg.
ViewPager.setOffscreenPageLimit(3);

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 fragment fires itself when using tabs

I have this Android architecture:
Activity that has an action bar with two tabs.
each tab is a fragment
my problem is, when i start the activity, the two tabs start working.
I don't want the second tab to start working unless i click on it.
How please?
note that i am using onCreateView on my second fragment
note
i tried to make my functions on onResume() but still fires itself
This comes from caching of the ViewPager : it will initiate the visible fragment and one on each side (left and right). There is no thing you can do against that. The method setOffscreenPageLimit will refuse a value below 1.
So the solution is to fire your own even to actually "start" your fragment when it becomes visible. Don't use the fragment lifecycle per say, but add a custom method : startBeingVisible().
To fire this custom event, you can just use an OnPageChangeListener.

Android changing setNavigationMode per fragment is crashing the app

I created the below project so you can see my exact code and what is going on:
https://github.com/CorradoDev/TabsTest/commit/8f054dab2371b791c4061ceb511413f720f65d67
Basically what I am trying to do is hide the tabs for some pages and show them in other pages.
Below is the code I am using to show the tabs in the onresume
if(getActivity().getActionBar().getNavigationMode()==ActionBar.NAVIGATION_MODE_STANDARD){
getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
THen to hide the tabs I am doing the below on resume:
getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
When I am on the first fragment(nothing in backstack). I can show and hide the tabs on hte second. It gives errors sometimes with changing tabs.
When I am on the second fragment in the backstack and I hide the third fragment. I see the second and third fragment both call the onrefresh but the third fragment does not show.
I am confused on what is going on and why this is not easier.
Below is the error I generally get
03-27 15:26:31.029: E/AndroidRuntime(5505): java.lang.IllegalStateException: Fragment already added: Fragment3{41f2e390 #2 id=0x1020002 fragment3}
I still would like to know why the above does not work. But my fix was to create another activity with the fragment and no tabs. That seems to work well. But I am interested if they did not intend you to change tabs and no tabs per fragment.
I had a similar situation - only that I used NAVIGATION_MODE_LIST instead of tabs. I run into similar issues when I called a fragment from another fragment e.g. click on a list item opening up the item details.
Now I call all fragments from the main activity which allows me to control the set up of the actionbar. Whenever the navigation list should disappear I just call NAVIGATION_MODE_STANDARD when the fragment is called and NAVIGATION_MODE_LIST for the other fragments.

Categories

Resources