ViewPager ,call fragments automatically - android

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.

Related

How to use an activity component in different fragments with viewpager

I have an activity with 5 fragments. i'm using different functionalities on the same component's onClick inside different fragments. because of viewpager's fragment preloading, functionality of next fragment is executed instead of current fragment. what should i do?
There is no way to avoid it because of view pager has minimim offset is 1.
So I would suggest to you using another way to archive your result:
Idea 1: If you are using FragmentStatePagerAdapter second parameter as FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, it means only one fragment inside your view pager will be in onResume state. Therefore you could start all of your networking/side effects job in onResume method.
Idea 2: Why do you using view pager, when you do not need swipe left/right effect? Probably you are in the wrong way. You could use for example just frame layout, then with your fragment manager attach and detach fragment when a user interacts how you expected. I would advise you to look better at FragmentTransaction. Here is a link.
Feel free to ask me about one of these approaches.

How to create fragment dynamically in viewpager

when we use fragment in viewpager, we alway add fragment when adapter call the constructor.so when the actvitiy call onCreate(), all the fragment would construct and call onCreate() at the same time.
but I want to construct the fragment when we slide in.what should I do?
For example:
I have 4 fragment: fragment1, fragmeng2, fragment3, fragment4.
at first, the viewpager would create fragment1, and when we slide into next fragment(right slip), the viewpager would create fragment2, and the fragment2 would call it onCreate().
How can I make it?
Thanks a lot for your help,so sorry about my pool English and I don't know have I explain the problem clarify.
Thanks again.
You can just move the creation of the fragment to the getItem() method in your adapter and return the newly created fragment.
My answer might be a bit lacking but I'm sure someone can complete it.
So what you can do is just create one fragment in the onCreate() of the activity. Implement the function onScrollChanged() and try to keep track there with getScrollX() where the user is exactly is in relation to the screen and just make some "if" statements that removes/adds the fragments as you are scrolling to the side.
As you have noted, viewpager actually loads the fragments at once (onCreate and onResume). If your intention is to do something before the next page is displayed, this link might give you an alternate way of doing this.
https://looksok.wordpress.com/2013/11/02/viewpager-with-detailed-fragment-lifecycle-onresumefragment-including-source-code/

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

onCreateOptionsMenu of Fragment called twice

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

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

Categories

Resources