Android - Honeycomb - Action Bar tabs and fragments - android

I am creating an app for Android 3.0+ that uses the action bar with tabs for navigation. I have 2 tabs that each load a ListFragment:
Tab 1 Tab2
A B
C D
Where A & C are list items in Tab 1 and B & D are list items in Tab 2.
Currently if you select Tab2 and then item B a new ListFragment is loaded on top of the current list fragment in Tab2. Pressing back takes you back to the original Tab2 contents. However if you don't press back and instead select Tab1 you see Tab1's contents on top of the new content you loaded in Tab 2. So I am guessing there is a better way of doing this. I wanted to use multiple fragments in each Tab to reduce the code complexity in each tab (for instance the onListItemSelected handling). Is there another way to do that without using multiple fragments? I am also trying to just save state but I am not sure how to tell the difference from when the user clicks back (then I would want to restore state) and the user has selected tab 1 and then selects tab 2 again (i would want to restore state).
Thanks

I am having similar struggles with the complexity of fragments and tabs on the Action bar. It seems to me that android for tablets is still very much in its infancy and there are various issues (like yours and the issues I'm having) that will be addressed in the coming months. For now, I think you just have to handle some of those complexities yourself instead of relying on the Android framework.
To answer your first question more directly, I think you need to step back and think out the functionality before diving into the code. I can give you a potential solution (keep track of your fragments manually in the activity and simply add and remove fragment as necessary via the ActionBar.TabListener interface.
Also, if you want the user to be switching from one fragment to another like you are suggesting, perhaps the back functionality shouldn't be used. What happens if the user clicks on the tabs 10 times in a row. Do you want the back button to take the user back through all of those events? I think the back button should only be used when the navigation system is very sequential and "back" is an easy concept. Check out all the Google apps that use tabs to see what I'm talking about.

Related

How can you have multiple instances of the same fragment in multiple pages of a ViewPager2?

I am creating an app in Kotlin. The desired operation is that when the user opens the app, the user will be presented with a menu offering navigation options. Upon tapping one of the icons, the user will then be presented with another menu. From that menu, several things can happen. This works fine in my app. The problem comes when I add ViewPager2. I would like the user to have multiple "pages" open at the same time that the user can switch between. Each page will start at the Main Menu and then move on as stated above. To simplify this, I'm trying to build something similar to a web browser. The user can have multiple pages, tabs, or whatever open which are independent of each other and can switch between them at will
Scrolling between pages in ViewPager2 is where most of my problems have happened. Here is what I have tried so far:
Loading each fragment into a "holder" fragment - The Fragment disappeared when scrolling to another fragment.
Letting the Main Activity load the Main Menu Fragment without a "holder" fragment. This worked for scrolling between pages, but when I tap on a menu icon on any page, it changed the data on the first page.
I tried using SupportFragmentManager and ChildFragmentManager. I tried to mix the two with varied results. None of which create the desired behavior.
Am I using ViewPager2 as it was intended or is there something else that I should be using to accomplish what I'm trying to do?

Android Navigate Component navigate to sub graph

I hava two Fragment(OverviewFragment、PersonalFragment), and one sub graph(contains two Fragment :BucketsFragment and ObjectsFragment, BucketsFragment is start destination). They are bind to one BottomNavigateView.
Now, After navigate to the sub graph start destination(BucketsFragment), and continue to navigate to ObjectsFragment. Then navigate to OverviewFragment and finally return to the sub graph.
Now, I came to start destination(BucketsFragment), but actually what I want is ObjectsFragment. what should I do?
The whole process is shown here
I want to navigate to the sub graph that retains the previous state, instead of a new sub graph.
As per the material design guidelines for bottom navigation:
On Android: the app navigates to a destination’s top-level screen. Any prior user interactions and temporary screen states are reset, such as scroll position, tab selection, and in-line search.
So technically, this behavior is expected.
When you're on the Resources tab and go to the ObjectsFragment, the back stack is
DashBoardFragment -> BucketsFragment -> ObjectsFragment
When you go back to the Overview tab, the back stack becomes
DashBoardFragment
And no state is saved for Fragments that aren't on the back stack. That's why when you reselect the Resources tab, it is recrated from scratch and you get back to
DashBoardFragment -> BucketsFragment
There's an existing issue for supporting multiple back stacks which aims to bring support for saving the state of each tab separately, allowing the behavior you wish. As per that issue, this requires significant changes to how Fragments work (since they are the one saving the state of Fragments) as well as integration into Navigation itself.
That issue points out a temporary workaround, demonstrated in the NavigationAdvancedSample where each tab uses its own separate navigation graph and separate NavHostFragment, thus allowing each one to keep its own state independently from one another. This requires a bit different of a setup in the MainActivity and the help of a set of NavigationExtensions to get that working.
It is expected that all of that functionality, once the multiple back stacks work is done, to be folded into the Navigation library itself.

Android - Navigate back to tabbed fragment

My Action bar consists of two tabs (a list of businesses and a map of businesses). If someone chooses from the list I would like to hide the tabs and show that business' page. If the user hits back, the business fragment is popped and the tabs should be displayed again.
What is the best approach to get this working? So far with the following code I have an inexplicable recursive loop if I pop the business fragment :(
So picture this, I'm displaying the BusinessListFragment tab. I choose a business, swap fragments and in onPause() I set the navigation mode to standard. Hit the back key, and in the onResume() of the BusinessListFragment I have this:
ActionBar ab = mHostingActivity.getSupportActionBar();
if (ab.getNavigationMode() == ActionBar.NAVIGATION_MODE_STANDARD)
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Which is supposed to bring back the tabs, but the app freezes with a loop. The tabs are definitely still present. Maybe I'm doing something else strange in my code (I'm sure I'm not though), or maybe Android does something strange when the naviagtion mode is set?
Edit: I've learnt that simply setting the navigation mode to tabs seems to actually select the first tab - which I believe would explain the recursion. Interesting!
Okay well this took a while to figure out but hopefully this will help future googlers :D
It seems the best practice is to never try and hide / show tabs by setting the navigation mode from within a tab fragment's onResume / onPause, or anywhere within it for that matter.
I therefore set the mode to Standard in the onResume() of the detail (business) fragment. Normally if there are no more hierarchical layers of navigation to traverse from within the detail fragment, it is fine to simply set the mode back to Tabs in the detail fragment's onPause() - accounting for back presses or up presses.
However in my case you can click on elements within the business page to launch another fragment (leaving a stack count of 3), meaning I cannot 'turn on' tabs within onPause(), and found the best solution was to do it within onDetach(). This should mean the tabs are only turned on when the detail fragment is actually popped from the stack.
Hopefully this solution is accurate, I have toyed with many ideas including back stack listeners and overriding onBackPressed, but for me this seems to be the best option.

Transition when switching Activity out of SlidingMenu (J.Feinstein)

I am an android newbie and I am trying to create an Android App with three Activities: a Homescreen, a listview and a detailview.
I integrated J. Feinstein's Sliding Menu in my Homescreen and filled it with a ListView to display my different categories (Restaurants, Bars, Culture, whatever). When I click one of my List Items, the new Activity gets created and Pops up. However, I want the Activity to replace the old one while the SlidingMenu stays on top and then gently slides away. I saw this behaviour in the Example App but I cant quite find the bit of code that holds the answer to my question. Hope anyone can help :)
The sample app uses fragment transactions instead of starting new activities to change the content. As far as I know, that is the only way to do the two actions – close sliding menu and change content – simultaneously.

Avoid Tabs from disappearing when searching

i've got a tabbed layout, and on one of the tabs i have a search functionality. When the user makes a new search, i need to show the results. However, doing so involves starting another activity to handle the search results.
this causes the tabs at the bottom to disappear. The user can get the tabs back by clicking on the 'back' button. But somehow, in the context of my application this can be a bit counter-intuitive and seems to be break the common layout flow.
is there any way to prevent the tabs from disappearing when invoking the search from one of the tabs?
thanks for any help/suggestions.
as far as i can understand your problem, You are not using tabs then you are using buttons. see some tabhost tutorials on how to create a tabbed activity. what you are doing is launching a new activity instead of just switching a tab in the existing one.
Also the other things you can look for are activity groups.
Hope this helps you somehow.

Categories

Resources