I am trying to dynamically update the title from a fragment. As the number of items in a list changes, the title would change accordingly.
I tried this
<fragment
android:id="#+id/myFragment"
android:name=".MyFragment"
android:label="{dynamicTitle}">
<argument
android:name="dynamicTitle"
app:argType="string"/>
</fragment>
from https://stackoverflow.com/a/55701078/1067596 and by changing the arguments at runtime.
This updates the title only on a navigation change or a configuration change.
Is there a way to force the navigation controller to refresh the title of the current fragment, from the argument without navigating away, or does it make more sense to access the toolbar and update its title manually?
Related
I am using Google's recommended single activity pattern with Android
Jetpack's Navigation component.
Fragment 1 is an authentication screen. After the user is authenticated and navigates to Fragment 2, I would like to make pressing of the Android back button close the app, and remove the back arrow in the app bar.
I have found methods, like onBackPressedDispatcher, to add / remove functionality from the back press, but nothing that also removes the back arrow.
I have also tried app:popUpTo="#+id/firstFragment" when navigating from Fragment 1 to Fragment 2, but that doesn't work either.
This should be possible to specify with a single line of code. Still trying to find. Any tips?
You need to remove fragment1 from back-stack when navigation to fragment2
fragment1
<fragment
android:id="#+id/fragment1"
android:name="packagenameforFragment1"
android:label="fragment1"
tools:layout="#layout/fragment_1" >
<action
android:id="#+id/action_Fragment1_to_Fragment2"
app:destination="#id/Fragment2_id"
app:launchSingleTop="true"
app:popUpTo="#+id/your_MainGraph_id"
app:popUpToInclusive="true" />
then when you navigate from fragment1 to fragment2 call this
findNavController(fragment).navigate(R.id.action_Fragment1_to_Fragment2)
to remove the back button from Fragment2 you can use this
in Activity onCreate()
val appBarConfiguration = AppBarConfiguration
.Builder(R.id.your_fragment2_id,R.id.any_other_ids_you_want)
.build()
then setup your toolbar like this
setupActionBarWithNavController(this, yourNavController, appBarConfiguration)
I have a jetpack navigation graph setup with bottom navigation and an action bar. The bottom navigation has 3 tabs. 1 of these tabs has a detailed fragment that I would like to display the back-arrow on in the action bar.
In MainActivity onCreate(), I've added:
supportActionBar?.setDisplayHomeAsUpEnabled(true)
This displays the back button on every tab & fragment.
How can I prevent display of the back button unless the user is navigated to a non-top level fragment? Is there a method that detects when this particular navigation has occured? If so, I presume I can do something like supportActionBar?.setDisplayHomeAsUpEnabled(false)
Additionally, is there a way I can display a custom back arrow on the child pages? The default arrow does not fit the UI.
Happy to provide more detail if needed.
You could use a combination of setupWithNavController(BottomNavigationView,NavController) and setupActionBarWithNavController(AppCompatActivity,NavController,AppBarConfiguration) from NavigationUI. The first will bind the bottom navigation to the navigation controller, the second will set up the support action bar to regard the navigation.
With the AppBarConfiguration.getTopLevelDestinations() you will be able to configure when to display the back arrow. The default is to only regard the root of the navigation graph as a top level destination, but you can also use another builder to define a set of top level destinations.
I have a project with the following 4 layouts:
I have actually 1 activity holding a bottom app bar and the NavHostFragment where the fragments get injected. The Main Fragment is the home view. There is a Management and a Setting fragment, both are top-level views like the home view but doesn't depend on each other. These 3 fragments can be switched by clicking an item in the nav drawer. For simplification, I'm trying the new navigation architecture component.
Now I have some design questions:
Should I move the bottom app bar into the fragments, cause they don't depend on each other and the FAB button has another action, otherwise I had to change the onClickListener in the activity when the fragments switch?
1.1 Or should I even show the bottom app bar in the management fragment? Maybe just the top bar with the Up caret.
1.2 Or bottom app bar + top bar and Up caret
1.3 and what about the drawer icon, should I display it in the Mgmt fragment
Should I use a fragment or an activity for the Settings fragment? When I use a fragment, I have to move the bottom app bar into the fragments. Otherwise, the bottom app bar would be visible in the Settings Fragment
The Management Fragment has just a recycler view. Clicking on an item should open a DetailView. Should I use a fragment or an activity here?
I read the doc about the navigation architecture component and there is a section about customizing the destination. Also, ich checked the source code and know that the fragments get replaced.
Further, I checked out some frequently used Apps how they implemented the navigation with a nav drawer and noticed, they all replace their fragments. Why does no one hide/show the fragments, is there a reason not to doing this?
Assume we have a fragment with a listview that holds data collected from a database or another expensive task. So wouldn't it be better to show/ hide these fragments instead of replacing it?
Sorry, it's my first app and I'm really confused about this topic, and it seems there are no official recommendations about it out there not even Material Design guidelines don't really make a reference about it.
How would you do it?
setupWithNavController on a Toolbar (or subclasses like BottomAppBar) only set up the Up icon and the title - they do not hook up menu items added to the Toolbar.
As per the Tie destinations to menu items documentation, you must set up your own listener and call onNavDestinationSelected(). For a BottomAppBar, that would be done by setting a Toolbar.OnMenuItemClickListener:
val navController = Navigation.findNavController(this, R.id.navigation_fragment)
myBottomBar.replaceMenu(R.menu.menu_with_nav_item)
myBottomBar.setupWithNavController(navController)
// Connect MenuItems to the NavController
myBottomBar.setOnMenuItemClickListener { menuItem ->
menuItem.onNavDestinationSelected(navController)
}
In my app I want to remove toolbar title for one fragment only. When I use the following code
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
it also removes title from all other fragments too. Is there any other way to remove title for only one fragment and keeping it in others. I also tried removing title attribute from navigation drawer implementation but then it starts showing app name in title. I don't want anything in this fragment . How this can be done?
I'm using one activity as a container and multiple fragments. Some of the fragments need to display app bar, some don't while others require to show a special app bar (e.g. an app bar that shrinks when swiping up). So where should I put the app bar (or action bar), in the activity or in the fragments?
By the way, if I put the app bar inside the fragments, how should I manage the app bars properly when fragments come in or move out?
if every fragment should have a different app bar, you probably want to use a Toolbar.
Toolbar is a standalone ActionBar that you can put anywhere, in any view group. The Toolbar API is roughly the same as the ActionBar one, so you should not have any major issue migrating to Toolbar.
For your app, you should have Toolbar in every fragment, when needed and every fragment control it's own Toolbar.