Without me pasting any code, i have an activity that shows three fragments using a viewpager to swipe between them.
When the activity is created, tab one is showing.. If i select the second tab and go back to the first tab, everything is fine. But if i select the third tab and then select the first tab again, the onResume method is called within the first tab and its causing some undesirable results... why would onResume only be called when coming from the third tab and not the second? Its like tab one is being killed when you swipe to tab three, but tab one is not being killed if you just swipe to tab two..
Without seeing the code I can only guess, but when you use tab you are going to be using a FragmentPagerAdapter or FragmentPagerStateAdapter this keeps reference to the fragments that are kept alive. I'm going to guess that you are using the FragmentPagerStateAdapter which only keeps certain fragments alive and destroys other fragments when they leave the view. So that when you are on the 3rd fragment, the first fragment is being destroyed, which is recreated and thus calls onResume when you go back and visit it. When you are on the 2nd tab, the fragment isn't destroyed and so when you visit it, it does not need to call onResume. Fragments keep one fragment to the left and one to the right "alive" even though they are not in view. So when you are on 1, 2 is alive. When you are on 2, 1 & 3 are alive, when you are on 3, 2 is alive. I hope this helps.
To add more to BlackHat's answer,
You should use FragmentStatePagerAdapter, if you dont already, instead of FragmentPagerAdapter.
Also you should set the OffscreenPageLimit property of yoru view pager to the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.
Related
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
When I first start the application it works correctly, but when application goes in background (by clicking home button) something goes wrong.
My Activity has a pager with 4 fragments, and when I reload Activity some of the fragments are lost (nothing is displayed).
I read that the pager keeps memory of the current fragment, the one on the left and the one on the right. How can I solve such a problem?
Am running in some weird problem. I have implemented ViewPagerTabStrip by following the tutorial Here
It's working fine and i have implemented Three Tabs.
On the first Page /tab , i am loading the data from server & displaying it in the ListView
The problem is, when i swipe to the Third Page & then i swipe back to Second page, the data of the first page is being loaded from the server again.( View of first tab is re-created.)
From the logs, it seems that this is the behavior of ViewPager & Adapter.
When we are on the first tab, it creates the view of second tab/ page as well in background & when we are on the Second tab, it was doing same for Third tab in background & upon reaching to third tab, it didn't created the view for it self(i.e third tab).
But when we swipe back to second tab, it re created the view of first tab as well.
How to solve this ? we can't afford to load the data from server again and again while swiping through the tabs.
I mean is there any way to stop the reload of data (or re-creation of view) while just swiping through tab pages? It should load the data when the whole fragment activity of View Pager is created and not at the time of swiping.
Any help on this please ?
Yeah, the ViewPager only keeps a certain number of fragments in memory before destroying them- this default number is usually two. Thus, what you have to do is tell the ViewPager to retain those fragments.
The simplest solution is to simply tell the ViewPager to keep three fragments in memory as follows:
viewPager.setOffscreenPageLimit(3);
Here are a few suggested answers on how to retain fragments (with more than just this method) and the reasoning behind the default behaviour as well as these solutions:
ViewPager and fragments — what's the right way to store fragment's state?
retain state of viewpager fragments android
I have got a weird issue with the 3 tabs of my ActionBar :
When i go from my second tab to the third, onCreateView() is not called, i like it because values in my third tab are in the same state.
Now, i will switch to the first tab and go back to the third, and now onCreateView() is called, and i don't want that...
Someone knows how to avoid this ? I don't understand why there is a difference between my second and my first tab !
Thank you for reading
It's probably because of the offscreenlimit in viewpager (I suppose it's your case).
It's set by default to one which means that view pager holds one fragment at each side of your currently visible tab in memory. (And when it's in memory it means that onCreateView() gets not called)
Scenario:
I have a fragment which has a ViewPager which contains 5 instances(different) of a single Fragment with different values, each having a listivew that contains some sort of items.
Problem:
The flow goes smooth when I click an item of listView(say on page1) but the moment I come back from there on pressing back (overridden OnBackPressed in the main_activity (will discuss later)), the viewPager fails to load the subFragments.
But when I switch to the 2nd or 3rd page, it gets displayed, and on going back to 1st page, it now gets displayed.
OnBackPressed():
I am maintaining a manual stack here. When I click on an item of ListView, the current Fragment Instance (of the parent Fragment ofcourse) goes into stack. And when user comes back , that instance gets popped off the stack and I replaces it on the activities FrameLayout container.
References:
Fragments not getting recreated by FragmentStatePagerAdapter after coming back to the main Fragment instance
Guys, I am really pissed off here, please help me out.
Frankly, I haven't worked much with fragments.
But it seems like the page is being thrown off the memory every time you switch to other pages. I am not sure but if there is a way for android to manage this and keep the page in its memory persistently, it might solve the problem
Finally I am able to get a workaround for this issue, which included two things:
while replacing the fragment make a call to
ft.replace(R.id.content_frame, frag).addToBackStack(null);
while initiating the ViewPager, the fragment manager to be used is :
mAdapter = new myAdapter(getChildFragmentManager());
Actually the problem is that, android fails to recognise and load the older instance of the fragment unless you explicitly add that instance to the backstack. So after adding it to BackStack, there is practically no need to maintain your own manual Stack, but you still can do that.