Android: Fragments in swipe view - android

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.

Related

Fragment initialized without onCreate being called

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.

How are transaction between two fragment in BottomNavigationView in Apps like YouTube so smooth?

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

Fragment swiping is slow in PagerSlidingTabStrip

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.

Android - Viewpager doesn't display content

I have a fragment that hosts a ViewPager with 3 fragments, 2 of them contain ListView (not inherits FragmentList)
The problem is:
If I click on one of the items in one of the list the application navigates me correctly to a new fragment, but, if I press the back button and go back to the ViewPager, a can see a blank fragment, only the first fragment who didn't have ListView is created, none of the other fragments are displayed, nor even their onStart is called
What's wrong? I suppose the fragment is not attached again, how can I solve this issue?

Android actionbar tabs: content of each tab is recreated everytime I click on that tab

I am developing an app that uses ActionBar navigation, I have 3 tabs (I dont use tabhost) as image below:
In the first tab, a fragment named FragmentListItem will be shown, if I click on Item1, FragmentListItem will be replaced by another fragment named FragmentItemDetails, that shows details of the selected item.
My problem is, when I select Tab2 or Tab 3, and then I reselect Tab1 again, what I get is not FragmentItemDetail anymore, it is FragmentListItem instead.
So, why is that? If I want FragmentItemDetail still be there instead of FragmentListItem, how to do that?
Moreover, when I press Back button (when Tab1 is selected), I want FragmentListItem will be shown again. I realise that Fragment class has no onBackPressed method to implement.
There are different solutions to this issue.
One possible solution is to use an own FragmentManager for this tab. This is possible with the getChildFragmentManager() method of Fragments.
Another solution could be a flag to identify which Fragment was shown as last on Tab1. If you know which Fragment was shown as last you could show this one by search this Fragment in your BackStack with the findFragmentByTag(tag) method of FragmentManager.
But I recommend you to try the first Option. If each tab has his own stack you have less work with your BackButton.

Categories

Resources