Using Fragments inside of Fragment - android

I am using 4 fragments in MainActivity with viewPager where viewPager is not scrollable, and I have four more fragments inside Second Fragment of MainActivity fragment. I am calling API in my all fragments in the onCreateView method. But when activity initialized so all fragments onCreateView method run, how to fix it because I want when the fragment is visible than my API call.
I already tried setUserVisibleHint method it's also working when activity initialized.
Should I use frame layout or anything else to fix this problem?

if i understood you good then you want to call your api when fragments are visible.
if yes then use
viewPager.addOnPageChangeListener
and onPageSelected(int position) callback then just make good condition which call you'r api
edit: changed adapter to viewPager

I think you need to put your api calls method in onResume in each fragment that will be fired when your fragment is on the top of fragments stack (Shown).

Related

call next fragment's lifecycle method when use viewpager2 with TabLayoutMediator

I use Viewpager2 With setup tablayout using TabLayoutMediator but the issue is
when I swipe viewpager first fragment to the second fragment here only call
current fragment's lifecycle methods, but when I click on tablayout
then call next fragment's lifecycle methods. I don't know why this happens?
This happens because the method that TabLayoutMediator uses to move to the selected fragment causes the RecyclerView to cache the next Fragment as it uses Smooth Scrolling.
This should not be a problem UNLESS you are doing something in the wrong part of the Fragment's lifecycle. If you only want do something when a Fragment is displayed then only do it in the Fragments onResume method as the behaviour you describe will only take the cached Fragment to "Started" state.

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/

ViewPager child view method callin automatic?

I implemented View Pager with fragment child A/B/C. To get selection of default Fragment(say A) I use mViewPager.setCurrentItem(position) and it work fine. But on this Second Fragment (B) view methods onViewCreated and setUserVisibleHint calling automatically.
Is there any way to prevent this !
You can try invoking ViewPager.setOffscreenPageLimit(0), which will no longer pre-load non-visible Fragments. The default value for the off-screen page limit is 1, so that should be the reason why the onViewCreated() and setUserVisibleHint() methods are being called for your other fragments.

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.

Categories

Resources