Fixed
Status Update
Comments
il...@google.com <il...@google.com>
mi...@gmail.com <mi...@gmail.com> #2
I have the same issue. There should be a built in method for setting title dynamically.
au...@gmail.com <au...@gmail.com> #3
I would expect a simple API inside the navigation architecture as this is quite a common design requirement
be...@gmail.com <be...@gmail.com> #4
To me, it is perfectly reasonable to remove the `android:label` attribute from the nav_graph.xml and set the title manually based on the arguments bundle.
md...@gmail.com <md...@gmail.com> #5
I'm currently working around this limitation by adding an "onNavigatedListener" to my root Activity and using the "destination.id " property against a lookup mapping to a desired title:
private val onNavigatedListener: (NavController, NavDestination) -> Unit = { _, destination ->
this.title = titleLookup[destination.id ]
}
private val onNavigatedListener: (NavController, NavDestination) -> Unit = { _, destination ->
this.title = titleLookup[
}
il...@google.com <il...@google.com>
ap...@google.com <ap...@google.com> #6
Project: platform/frameworks/support
Branch: androidx-master-dev
commit a958e3d36aeca8767d1c3cb53caa55ba3dd1c65d
Author: Ian Lake <ilake@google.com>
Date: Wed Nov 28 16:06:01 2018
Allow {argName} parameterized labels
Support parameterized labels similarly
to how ActivityNavigator uses dataPattern by
using {argName} blocks in the label.
Test: testapp works
BUG: 80267266
Change-Id: I923f3a6e11662c4364d4b3f0757361edea016cec
M navigation/integration-tests/testapp/src/main/res/navigation/nav_main.xml
M navigation/integration-tests/testapp/src/main/res/values/strings.xml
M navigation/ui/src/main/java/androidx/navigation/ui/AbstractAppBarOnNavigatedListener.java
https://android-review.googlesource.com/837143
https://goto.google.com/android-sha1/a958e3d36aeca8767d1c3cb53caa55ba3dd1c65d
Branch: androidx-master-dev
commit a958e3d36aeca8767d1c3cb53caa55ba3dd1c65d
Author: Ian Lake <ilake@google.com>
Date: Wed Nov 28 16:06:01 2018
Allow {argName} parameterized labels
Support parameterized labels similarly
to how ActivityNavigator uses dataPattern by
using {argName} blocks in the label.
Test: testapp works
BUG: 80267266
Change-Id: I923f3a6e11662c4364d4b3f0757361edea016cec
M navigation/integration-tests/testapp/src/main/res/navigation/nav_main.xml
M navigation/integration-tests/testapp/src/main/res/values/strings.xml
M navigation/ui/src/main/java/androidx/navigation/ui/AbstractAppBarOnNavigatedListener.java
ap...@google.com <ap...@google.com> #7
Project: platform/frameworks/support
Branch: androidx-master-dev
commit 5e475aa0a00390755a9d4a9260c827b72ec81eb2
Author: Ian Lake <ilake@google.com>
Date: Wed Nov 28 15:48:29 2018
Pass Bundle of arguments to OnNavigatedListeners
Instead of just passing the NavDestination,
pass both the NavDestination and the (optional)
Bundle of arguments. This allows listeners to
dynamically update their content rather than only
relying on the fixed data from the NavDestination.
Test: testapp still works
BUG: 80267266
Change-Id: I80f0e65526711a2ba11b7a12fc907a1b9dbe83e9
A navigation/common/src/main/java/androidx/navigation/NavBackStackEntry.java
M navigation/integration-tests/testapp/src/main/java/androidx/navigation/testapp/NavigationActivity.kt
M navigation/runtime/api/1.0.0-alpha08.txt
M navigation/runtime/api/current.txt
M navigation/runtime/src/main/java/androidx/navigation/NavController.java
M navigation/ui/src/main/java/androidx/navigation/ui/AbstractAppBarOnNavigatedListener.java
M navigation/ui/src/main/java/androidx/navigation/ui/CollapsingToolbarOnNavigatedListener.java
M navigation/ui/src/main/java/androidx/navigation/ui/NavigationUI.java
M navigation/ui/src/main/java/androidx/navigation/ui/ToolbarOnNavigatedListener.java
https://android-review.googlesource.com/837142
https://goto.google.com/android-sha1/5e475aa0a00390755a9d4a9260c827b72ec81eb2
Branch: androidx-master-dev
commit 5e475aa0a00390755a9d4a9260c827b72ec81eb2
Author: Ian Lake <ilake@google.com>
Date: Wed Nov 28 15:48:29 2018
Pass Bundle of arguments to OnNavigatedListeners
Instead of just passing the NavDestination,
pass both the NavDestination and the (optional)
Bundle of arguments. This allows listeners to
dynamically update their content rather than only
relying on the fixed data from the NavDestination.
Test: testapp still works
BUG: 80267266
Change-Id: I80f0e65526711a2ba11b7a12fc907a1b9dbe83e9
A navigation/common/src/main/java/androidx/navigation/NavBackStackEntry.java
M navigation/integration-tests/testapp/src/main/java/androidx/navigation/testapp/NavigationActivity.kt
M navigation/runtime/api/1.0.0-alpha08.txt
M navigation/runtime/api/current.txt
M navigation/runtime/src/main/java/androidx/navigation/NavController.java
M navigation/ui/src/main/java/androidx/navigation/ui/AbstractAppBarOnNavigatedListener.java
M navigation/ui/src/main/java/androidx/navigation/ui/CollapsingToolbarOnNavigatedListener.java
M navigation/ui/src/main/java/androidx/navigation/ui/NavigationUI.java
M navigation/ui/src/main/java/androidx/navigation/ui/ToolbarOnNavigatedListener.java
il...@google.com <il...@google.com> #8
This is fixed internally and will be available in 1.0.0-alpha08.
You'll be able to use labels such as "Hi {name}!" and the NavigationUI methods will automatically populate the {argName} elements with the appropriately named argument.
You'll be able to use labels such as "Hi {name}!" and the NavigationUI methods will automatically populate the {argName} elements with the appropriately named argument.
an...@gmail.com <an...@gmail.com> #9
Sounds good. I understand how I would pass the argument values during normal navigation, but how would I pass the argument to the starting fragment (set in `app:startDestination`)?
il...@google.com <il...@google.com> #10
Passing arguments to the start destination of your graph was added in alpha07: https://issuetracker.google.com/issues/110300470
an...@gmail.com <an...@gmail.com> #11
Am I right in determining from that, that the starting fragment can only have it's title passed during initial creation[$1] or direct navigation[$2]?
Assuming I have a starting fragment labelled "A", and a second fragment somewhere in the graph labelled "B".
If I wish to change the title for the starting fragment "A" due to some action taken (e.g button press) on that ("A") fragment then I will need to find some other means of setting it, is this correct?
Same if I want the title on fragment "A" to change based on a choice made on fragment "B", but navigation back to "A" (from "B") occurs via the back/up button or popping from the nav stack?
At the moment I'm setting the title for my starting fragment through a navigation listener (addOnNavigatedListener), and observing a LiveData change from my view model, as doing only one or the other results in situations where the title is not set. This means that for the majority of the time the title is set at least twice [$3].
$1) Presumably any one of these is only called once during the app's active lifetime, likely in `[Main]Activity.onCreate`, is that correct? As I understand it, using this feature would also require some alteration to how navigation is set up in the layout (.xml) files, right?
* `NavHostFragment.create(R.navigation.graph, args)`, or
* `navController.setGraph(R.navigation.graph, args)`, or
* `navController.setGraph(navGraph, args)`
$2) `navController.navigate(action)`
$3) The title is set by
1. the navigation component to whatever is in the label, and
2. the onNavigated listener, and
3. the observer on the LiveData.
While it's light work this repetition in setting the title seems wasteful.
I cannot quite put my finger on it but it seems strange to me that the navigation source gets to set the title as opposed to the destination setting the title based on some internal state.To make this work for me I would have to ensure that every source a fragment can be navigated from knows how to correctly determine the expected title, as opposed to that determination only being made in one place (the destination fragment).
il...@google.com <il...@google.com> #12
Parameterized labels only pull from the arguments of the destination, yes.
If you need dynamic titles or more complicated logic, you should not use android:label in your graph and instead set the title yourself - NavigationUI only sets a title if the android:label exists.
If you need dynamic titles or more complicated logic, you should not use android:label in your graph and instead set the title yourself - NavigationUI only sets a title if the android:label exists.
an...@gmail.com <an...@gmail.com> #13
Thanks, I've removed the label from the fragment in the nav graph resource file. It was used as the default/fallback title until the title was set through the user's action.
se...@google.com <se...@google.com> #14
This API was reviewed as part of "mass-review" of Navigation APIs at Donut Pod on December 4th.
jo...@gmail.com <jo...@gmail.com> #15
Sorry to come up and notify you all but this is marked as fixed even though I found an issue with this approach:
<fragment
android:id="@+id/exampleFragment"
android:name="com.example.ExampleFragment"
android:label="{title}"
tools:layout="@layout/fragment_examplel">
<argument
android:name="title"
app:argType="string"/>
</fragment>
This works fine, but as soon as the activity/view gets destroyed (e.g. rotating the device) the title gets cleared. let me know if I'm missing something but I'm using the safe args direction and passing the text as getString(R.string.example_title).
<fragment
android:id="@+id/exampleFragment"
android:name="com.example.ExampleFragment"
android:label="{title}"
tools:layout="@layout/fragment_examplel">
<argument
android:name="title"
app:argType="string"/>
</fragment>
This works fine, but as soon as the activity/view gets destroyed (e.g. rotating the device) the title gets cleared. let me know if I'm missing something but I'm using the safe args direction and passing the text as getString(R.string.example_title).
il...@google.com <il...@google.com> #16
Re #15 - please file a new bug with a sample that reproduces your issue.
st...@gmail.com <st...@gmail.com> #17
can i use argument from a parcelable type for the label ?
something like this ?
<fragment
android:id="@+id/displayFragment"
android:name="com.commonsware.jetpack.sampler.nav.DisplayFragment"
android:label="{User.userName}" >
<argument
android:name="User"
app:argType="com.example.app.User"/>
</fragment>
something like this ?
<fragment
android:id="@+id/displayFragment"
android:name="com.commonsware.jetpack.sampler.nav.DisplayFragment"
android:label="{User.userName}" >
<argument
android:name="User"
app:argType="com.example.app.User"/>
</fragment>
il...@google.com <il...@google.com> #18
Re #17 - no, that's not supported, the only supported format is {argName}
which calls toString()
on the argument. You'd want to file a new feature request.
Description
The navigation components use the value in `android:label` to set the [toolbar] title depending on where in the navigation graph the use has navigated to. This works fine except I want to have the title reflect the category of things the screen is referring to, but the specific thing that the user is about to edit.
Normally I would have piece of code like `updateTitle` below on the activity and then call it from the fragment (onResume) with:
``` kotlin
updateTitle(getString(R.string.book_title, bookNumber))
```
but this does not work unless I also remove the `android:label` attribute from the nav_graph.xml.
Component used:
'android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha01'
'android.arch.navigation:navigation-ui-ktx:1.0.0-alpha01'
Version used:
as above
Devices/Android versions reproduced on:
All
- Sample project to trigger the issue.
``` xml
// snippet from nav_graph.xml
<fragment
android:id="@+id/bookFragment"
android:name="com.example.bookFragment"
android:label="@string/book_title" >
<argument
android:name="bookId"
app:type="integer" />
</fragment>
// snippet from sttrings.xml
<string name="book_title">book %1$d</string>
```
Some snippets from the way I would otherwise do it.
``` kotlin
// actrivity, implements updateTitle( from an interface (Foo)
override fun updateTitle(title: String) {
toolbar?.title = title
}
// fragment
override fun onResume() {
super.onResume()
// foo is the activity cast to Foo
foo?.updateTitle(getString(R.string.reading_title, 1))
}
```
- A screenrecord or screenshots showing the issue (if UI related).
Actual title vs expected