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);
Related
So I have a fragment inside a viewpager which is contained inside a fragment which is getting initialize'd as shown in the debugger but doesn't have it's onCreate,onCreateView or any such methods being called. The activity containing has a bottom navigation view and contains 4 such fragments and this issue is only happening in 1 such fragment.
All these fragments and viewpagers and fragments inside them are created on the oncreate of the activity. If I move the logic to create the fragment to when a bottom tab is clicked, this issue gets solved on.
How is this possible ?
Only the currently active fragment and the two directly adjecant fragments are created when the viewpager is first shown. so the fragment for the first page, the fragment for the page shown if you "scroll to the left" and the fragment for the page shown when you "scroll to the right". the 4th fragment will be created when it is put in the next adjecant position.
so if you have fragments a,b,c,d in a viewpager like -a-[b]-c-d- where b is the first visible page, only a,b and c will be created at startup. when you scroll to c -a-b-[c]-d- onCreate for fragment d will be called.
You can control number of pages/fragments preloaded when you launch your activity by setting it's off Screen Page Limit on your view pager adapter. like this
mViewPager.setOffScreenPageLimit(limit)
If you set limit to 0 than only first fragment would be load.
https://spotandroid.com/2016/11/23/android-tricks-viewpager-and-offscreen-tabs/
https://techcodegeek.wordpress.com/2015/06/23/android-viewpager-and-performance-improvements/
Need more detail to know your issue.Issue which you are facing is not understood with information you have provided.
i have an Activity which haves several Fragments and am using PagerAdapter for organising this Fragments , i got to know when my activity, loads all of my Fragment's codes also gets executed even when am at first Fragment at that time my whole codes of all fragment 2 or 3 also got executed which are in the onCreateView of my Fragment so can we stop this behaviour i want to execute this codes only when user selects that particular fragment
you can limit your pager by calling this method default load number is 3
yourPager.setOffscreenPageLimit([number of pages]);
if you want just one page without preload try this link by TejjD
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?
I'm trying to send data to 3 other fragments in a Tab of 4 from the first Tab which gets the data from the server, but observing the creation of the Tabbed Fragments, First and Second is Initialized, so the second doesn't get the data and the third and fourth get it. Even, when I tried to load the data from onResume in the second Fragment Is there anyway I can let tell the Second Fragment should reload itself when the Tab is clicked. Any help would be appreciated
Maybe using OnPageChangeListener would when you detect when the SecondFragment becomes active.
I would generally recommend notifying such things through the parent Activity
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.