Android - Navigate back to tabbed fragment - android

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.

Related

I want to keep AddTransactionFragment while I navigate to other fragment with BottomNavigation (Android, Kotlin)

My problem is illustrated by this screen record.
When I click the FAB on FinanceFragment, it will open AddTransactionFragment. But when I navigate to ReminderFragment and go back to FinanceFragment, the AddTransactionFragment is destroyed.
How could I -- similar to the YouTube app when one switches between fragments -- keep AddTransactionFragment visible while I navigate to another fragment?
I tried to add addToBackStack() but this turned out not to be what I was looking for.

Back vs. Up - Intended behavior

The navigation design guide explains:
When the previously viewed screen is also the hierarchical parent of the current screen, pressing the Back button has the same result as pressing an Up button—this is a common occurrence.
up vs back - navigation guide
I have a MainActivity A which opens another activity B when touching a navigation entry in the NavigationDrawer. Activity A is set to be the parent of activity B in the AndroidManifest: android:parentActivityName=".MainActivity"
I followed this android documentaion to add up navigation to activity B. It shows how to implement onOptionsItemSelected in activity B:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
// Respond to the action bar's Up/Home button
NavUtils.navigateUpFromSameTask(this)
return true
}
}
return super.onOptionsItemSelected(item)
}
When I press back from Activity B the state of Activity A was saved and the NavigationDrawer is opened. If I use the up navigation though, onCreate()of activity A is called and it lost its state (the drawer is closed etc.).
This is not the quoted "same result".
When I replace the NavUtils.navigateUpFromSameTask(this) with a simple finish() it has the same behavior as pressing back - the state of activity A is kept.
Naturally I would prefer the way using finish. So what is the intended behavior? Do the guides contradict each other or am missing something?
It is an unfortunate reality that Google leaves documentation up for longer than it is relevant, and will even post two different pieces of documentation that directly contradict each other.
In the case of the Up button, your link says
The Up button appears in the app bar and is used to navigate within an app based on the hierarchical relationships between screens. [...]
The Back button appears in the system navigation bar and is used to navigate, in reverse chronological order, through the history of screens the user has recently worked with. It is generally based on the temporal relationships between screens, rather than the app's hierarchy.
However, there is also this article, which says
When the system Back button would not exit your app, such as when you are on your own task and not on the start destination, the Up button should function identically to the system Back button.
So... which one should you trust?
I assert that you should trust the second one. The first one was posted years ago; I don't know its exact age, but you can tell that it's old because the screenshots all use the Holo theme. The second one, on the other hand, is part of Android's Architecture Components, so is significantly newer. In general, I'd go with the newest piece of documentation.
Additionally, I think that Google was wrong to say for all these years that the Up button should work differently from the Back button. As someone who spent a lot of time thinking about navigation in my app, I see where they were coming from, but real-world users always get confused when Up did something "different".
So I'd go ahead and just finish() your activity when the user presses the Up button, and not worry about those two articles you found.
I think the change of policy to suggest consistency between the up icon and the system back button is a good idea. However the advice that you should:
"navigate in reverse-chronological order through the history of screens"
Is too crude, or at least should be clarified what they mean by "screen".
eg. When you have a bottom nav bar, the back button / up icon should take you back up the hierarchy within your tab section before jumping to the section previously visited. And you should preserve the previous state when revisiting a tab section (which may be drilled down into a lower level screen).

Android Actionbar tabs Navigation

I am a beginner in android. I have created Tabs using ActionBar tabs. Now I want to implement, when the user in in first tab and clicks back Button activity should close or when the user is in second tab, he clicks the backbutton the app should show the first tab, which will make it as the home tab. I tried using popupbackstack() but it always closes the activity no matter what tab i am in.
please guide me. Thanks in advance.
The easiest way would be record the tab state (i.e., which tab is showing) in the host activity, and override the onBackPressed() method to do what you want.
Some tips:
remember to save the state(override the onSavedInstanceState() and save the tab state) to make you activity works right after restore from onPause().
read about android's recommended navigation style here. What you designed is a little bit different from what android suggests.

Android - Honeycomb - Action Bar tabs and fragments

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.

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