Does the backstack only exist in the FragmentManager instance? - android

So if for example I had 4 fragment managers, would there be 4 seperate back stacks to handle?
Im looking to create a tabbed application that uses a bottom nav bar to switch between fragments. But within each tab the user can progress through to different areas of the app. I need it so that no matter how far they get in to a tabs flow, if they clicked on another tab, then came back to the previous tab, they would still be at the same place they left off.
So would using multiple FragmentManager instances (one for each tab) solve my issue?
In my case I have 4 fragments, one for each tab. So if im on tab 1 and go to fragment 4 in tab 1, if I then tap tab 3 and go to fragment 2 in tab 3, if i then go back to tab 1, i should still see fragment 4 of tab 1. But im not sure how to set this up.

You should be fine using two Fragment instances for the tabs in the nav bar, and use each tab Fragment's getChildFragmentManager() method

Related

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

Adding TabBar using fragments in android

I have a no. of activities in my app.
Now i want to add tab bar at bottom using fragments in android studio.but my problem is that when i open new fragment inside
current tab on button click and then switches to other tab.then when
i come back to same tab it loads the first fragment and not the one
at which i switched to other tab.
Is there any solution for this.
4.I have tried transaction and fragment manager tutorial for this but could not succeed.
Try Bottom Tabs in Fragment State Pager Adapter

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.

Control two fragments within a tab

I was hoping someone could point me in the right direction when using fragments within a tab. I have a mutiple tab application. On one of the tab pages I have broken down the content into two separate fragments.
The issue is I do not want the fragment activity I using to control the tab to control also control the two fragments being displayed. I want an something between the two elements such as another fragmentactivity to control the fragments used as content.
Is this possible and if so how?
Tab Fragment
tab 1 tab2 tab 3
tab2 : display is two fragments
tab2 -> anther fragmentActivity -> two displayFragments
Transition to FragmentActivity
I believe I've found my answer. This can't be done. My fragments need to response to a fragmentActivity and the fragmentTransaction only transitions between fragments not activities. So they will have to respond to my activity controlling the tabs.

Android FragmentTransaction, ActionBar Tabs, and Multiple Fragments

So, I am building a tablet app with the compatibility library and have run into an oddity I can't seem to figure out. All in one activity, I have 2 tabs (Tab A and Tab B), and 3 Fragments (Fragments A1, A2, and B). The ActionBar.TabListener associated with Tab A handles the adding and removing of Fragments A1 and A2, and the ActionBar.TabListener associated with Tab B handles the adding and removing of Tab B. So far so good.
The strange behavior is exhibited when I launch the activity (so Tab A is selected and Fragments A1 and A2 are displayed from left to right, correctly) click on Tab B (so Fragments A1 and A2 are removed and Fragment B is shown, still correctly) and then click back on Tab A!
Now, Fragments A1 and A2 are showing, but in the reversed order: A2 and then A1!
Has anyone experienced this oddity? If I select Tab B and then Tab A again, they reverse again to be in the correct order, and the cycle continues. According to this article, "If you're adding multiple fragments to the same container, then the order in which you add them determines the order they appear in the view hierarchy", which strangely doesn't seem to be the case now does it.
Any ideas? Thanks in advance!
According to this article, "If you're adding multiple fragments to the same container, then the order in which you add them determines the order they appear in the view hierarchy", which strangely doesn't seem to be the case now does it.
Personally, I wouldn't count on that.
For example, let's assume that you are using a horizontal LinearLayout. Your current code presumably is putting both fragments in the LinearLayout. The way I approach it is to have two FrameLayouts already in the LinearLayout, and to put each fragment in one of the FrameLayouts.

Categories

Resources