Friends
I have 4 tabs A, B , C, D in my App All 4 tabs are fragments , For tabs I am using PagerSlidingTabStip, In view Pager adapter I am using FragmentStatePagerAdapter
My problem is when I toggle between TAB A and TAB B it is very smooth but once I click TAB C it takes time and its also called Fragment D
Now If I toggle between TAB C and TAB D again it is smooth but if I click on TAB A or TAB B then again it takes time and if I click on TAB B, TAB A also get called in background which I can see from log cat
I am using loader manager in Fragment A , B , and D to loader data from database .
I have no idea why it is happening, Can Any one please explain me , it making my app slower in the last stage when I am about to release
You can use setOffscreenPageLimit (int limit) for you view pager.
if you follow this link it will help you out.
http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit(int)
What happens is that your fragments are created lazy upon need - If you're on A then B is created and scrolling between them is smooth. Once you jump to C it's created together with D - that's why scrolling between them is smooth.
The reason it's not smooth, is that the creation of your fragments is heavy. You should check your onCreateView or example to find out any heavy lifting there. Maybe there are stuff that can be moved into the BG there.
Important is that the fragment that contain array list or whatever you are passing into it are executing in the same activity in which you are passing the fragment that's why your fragment are slowly swiping, i just take 3 days to find out the solution. so the solution is that execute your data or load your data in to main class from where your are calling your fragment or from view pager whatever from where you are calling your fragment.
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 am making an application that has MainActivity that contains BottomNavigationView and FrameLayout above it. There are 3 Fragments say Fragment A, B, and C.
My doubt is, How do I make switching of Fragments as quick as YouTube Android application? By saying quick, I mean that, when I am on "Home" tab of Youtube application and I switch to the "Trending" tab and again go back to the "Home" tab, it simply loads "Home" tab within fraction of seconds, as if it just hided the inflated page in background and showed up when selected from BottomNavigationView. And also, It inflates the page exactly to the same position where I left.
When I am trying to implement the same in my Application, the RecyclerView in Fragment A re-inflates if I come back from Fragment B.
I am expecting the idea how they do it and in which method they do it (For eg. onStart or onDestroy or onViewCreated)...
If you are using viewpager then Increase the viewpager offset limit
viewpager.OffscreenPageLimit = 2;
Tt's limit is one by default. I hope this may fix your issue.
Thanks
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 have an activity with 3 tabs. Tab A, Tab B, Tab C. Tab A loads data from a server. Tab B and C have text fields on them with buttons. Each tab is implemented with a different fragment.
My issue is when I type on a text field in Tab B and swipe to Tab C, the text field from Tab B is not cleared. I would like for it to be cleared. Is there a way to recreate or reload the fragment when I swipe to the selected tab?
For Tab A to finish loading data from the server takes some time, so I created a progress dialog while the operation is running. Now the progress dialog works correctly, but if I swipe from Tab A to Tab B to Tab C back to Tab B, the progress dialog is shown. The progress dialog is shown in the onCreateView() method of the fragment for Tab A. I don't understand how this is possible. I think it is because the fragments are stilling running even when I am on different tabs. Is it possible for the fragments to only run/start when the user swipes to the selected tab? Also, is there any code that I should run in the onTabUnSelected() method? Is there a way to end a fragment in that method?
Edit:
I figured out how to have the text fields cleared when I leave the fragments. I overrided the following:
public class MyFragment extends Fragment
#Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (!visible) {
...
}
}
Now the text fields are cleared when I swipe from tab to tab. I'm not sure if this is the best way to solve the problem, so if somebody else has a better way, please let me know. I tried to implement the same function in the other fragment to dismiss the dialog when the fragment is not visible, but it does not work.
Edit 2:
For some reason, the onCreateView() method for fragment A is being run when swiping from Tab C to Tab B. Fragment A is loaded on Tab A. Shouldn't the onCreateView() method for fragment A only be run when Tab A is selected?
Edit 3:
I figured out what the problem. Android automatically sets the value of setOffscreenPageLimit(int) to setOffscreenPageLimit(1). This automatically loads the next tab in the background. So as a workaround, I will create a progress bar/spinner on the screen itself instead of creating a progress dialog.
I will try answer your queries one by one.
See this link and this link to learn how to communicate between fragments. i found second link (vogella) more useful practice.
its is absolutely fine that on tab A is working in background on swiping to Tab B and C. You need to understand that your all 3 tabs belongs to one activity that may contain three different fragments.
I'd like to cache a fragment view. My Activity has swipeable tabs and each tab calls a different fragment. But when i swipe between tabs the transition seems a quite slow because of the destruction of the fragment view, that is rebuilded during the swipe operation. Does anyone know how can i cache the view of each fragment to prevent this issue?
I work with library support v4 and api 14
I tried to implement a constructor for the fragments, called by the activity container of the fragments: i call the constructor, the fragments are created as variable of the activity class and then, whenever a fragment has to show itself, the activity class returns the fragment object i created before, but this doesn't improve my application a lot because the view of the fragment is destroyed anyway
This is because internally by default the pager loads a maximum of 3 pages (fragments) at the time:
the one displaying, previous and next so if you have 5 fragments this will happen while you move from first to last: (where x is a loaded fragment)
xx000
->
xxx00
->
0xxx0
->
00xxx
->
000xx
Try using
myPager.setOffscreenPageLimit(ITEMS_COUNT-1);
This will tell the pager to keep all of them in memory and not destroy/create with every swipe (keep a close look on the memory management)