onCreateOptionsMenu of Fragment called twice - android

Scenario:
I using a ViewPager inside a Fragment. This ViewPager inflates 3 fragments (AFragment, BFragment, CFragment) pages using FragmentPagerAdapter.
I have printed logs inside
onCreateOptionsMenu
onPrepareOptionsMenu
of all 3 fragments.
Problem:
For AFragment when created onCreateOptionsMenu was called only once,
but when I swiped to BFragment onCreateOptionsMenu and
onPrepareOptionsMenu for BFragment were called twice. And similar
thing happened with CFragment.
Can anyone explain why this is happening and How I can avoid it?
Thanks.

Because android by defaults loads a next and previous fragment in background, in order to load fragments efficiently and to remove lag.
However you can customise the same by using setOffscreenPageLimit (int limit), Which is used to set the number of extra fragments you would like to load in background. By default its set to 1.
Read Android fragments strange load for more information

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

Android Fragment Life Cycle

I have a action bar with 3 navigation tabs: Fragment 1, Fragment 2 and Fragment 3. Now, I want to do a task whenever Fragment 3 selected, so I put my task code in onCreateView() method. However, I find that Fragment 3 does not do the task, it means the onCreateView() method is not called. (I check this by logging). The other strange things is:
- When I slide: F2-> F3: the task not work.
- When I slide: F1->F2->F3: the task work. (onCreateView() method called)
I don't know why F3's onCreateView() method called when I slide from F1 to F3?
Any ideas for this?
The remark from tyczj is correct but it does not give a solution to the problem.
In your F3, just override setUserVisibleHint(boolean) and when boolean is true, it means that F3 is now visible inside the ViewPager.
Note that you can rely on this method because you are using a ViewPager and it properly set the user visible hint when a fragment is shown.
When you are not using ViewPager, you can't rely on this method unless you are explicitly calling the method when you know that the fragment is visible.
EDIT : setUserVisibleHint() is not called by the ViewPager, but by the FragmentPagerAdapter.
Because a ViewPager loads the next view so that it is ready when you want to scroll to the next fragment. by default the viewpager loads the previous current and next fragment in the ViewPager so when you scroll from F1 to F2, F3 is going to be loaded in so that you have a smooth scroll when you go to F3
The previous answer tells you why what you are doing won't work. But to correct it you will want to create a AsyncTaskLoader and use the fragments LoaderManager to load and manage the task. See http://developer.android.com/guide/components/loaders.html

Where should I code in Fragment that it will run only once- android

I am implementing Tab Layout with Swipeable Views in android. For implementation I had followed
AndroidHive
and
Tabs and swipe views.
But with both, I am facing the same problem. I am having 3 fragments but when my application run, onCreateView of 1st and 2nd Fragment called instead of only of 1st fragment's. When I swipe and go to 2nd fragment, onCreateView of 3rd Fragments get called.
So, whatever I code in 2nd fragment execute in the first fragment view. I researched and came to know that it happen to keep the next fragment into memory for smooth animation. But I am wondering, where I would code in the fragment so that it will execute only once or how to restrict Fragment getItem() method to be called only once. What can be the solution for this?
According to the Fragment's documentation you can implement the onCreate() link:
The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
This should fix your problem, because i guess you now only use the onCreateView which might be called more than once.
For more information you can check the Fragment's lifecycle or documentation

ViewPager ,call fragments automatically

Good afternoon!
I have a method which crashed with NullPointerException if I call it immediately after launch , but If i slide through all fragments and then call the method, all works good.
As far as as consider, smth not initialize.
I need to fix this bug fast, so I want to slide through all fragments programatically between UI launch(in onCreateView or smth else). How can I do this ?
Here is RootFragment http://pastebin.com/VfmRbVpc
One of child fragments http://pastebin.com/XnftaqTH
I have a NullPointer in child fragment line 63
elview = (ExpandableListView) v.findViewById(R.id.expand_lv);
Thanks!
Fragments in a ViewPager are created on in a lazily manner so If you need access to a Fragment thats further down the line, you should attach a OnPageChangeListener to the ViewPager and execute the needed code when you reach the target index/position.

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