android fragment fires itself when using tabs - android

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.

Related

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);

How to make tabs (ActionBar) stop calling onCreateView() of its neighbors tabs

I need my first tab, when I select it (after select the second), to call its onCreateView() or just its onResume(), or another function, to update it.
I have 2 tabs, what I do in the second tab have to change something in the first tab. But, when I'm in the first tab and I select the second, the onCreateView() of the first is called again. So, if I do something in the second tab and return to the first, nothing changes. To update the first, I had to go to the second again, to it call the onCreateView() of the first and then I have it updated.
I need the first tab to update itself when I select it, not when I select the second tab. I already tried the ViewPager.setOffscreenPageLimit(0), with no success.
How can I make it work?
I'm assuming here that you're using a ViewPager. The reason setOffsetPageLimit(0) doesn't work is because 1 is the minimum value here.
You should create a callback that your Activity (or a class which can access both Fragments) implements, which the second Fragment can tell to update. You can then do logic in that callback to update your first Fragment.
I'd advise not doing the update in onCreateView() as you've mentioned because then it ties updates to the Fragment lifecycle - which is tricky to control.

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.

onTabReselected for actionbar tab getting called when unwanted

I followed instruction for actionbar Tab from here
I'd like to user to be able to scroll back to top of list-view when they reselect a tab.
I put listView.setSelection(0) inside onTabReselected(Tab,FragmentTransaction) method.
It works as intended when I re-click the tab already chosen.
But the function is also called when I didn't expect it to be.
I start a new activity.
When I come back to the original activity(with the tab), the onTabReselected function is called.
How can I differentiate the two cases so that I don't scroll the list when coming back from another activity?
The unexpected call to onTabReselected was due to my setSelectedNavigationItem call in onResume.
problem solved.

Why are both tabs running in FragmentTabs?

I'm using FragmentTabsPager, and when my app starts, both tabs initiate net-requests to fetch their content....I would expect the first tab to load onAppStart, and the second tab to load when it is clicked, this is not the case as they are both loading simultaneously onAppStart why is this the case?
Each tab within a FragmentTabsPager runs a Fragment, which is essentially an activity within a single view. It has its own lifecycle and behaves similarly to an actual Activity.
Initialize your Fragment from the onTabClick (or equivalent event) in the parent

Categories

Resources