Status Update
Comments
dm...@gmail.com <dm...@gmail.com> #2
gr...@ynab.com <gr...@ynab.com> #3
Great! Thanks a lot, I'll look for the live updates soon!
il...@google.com <il...@google.com> #4
From talking offline, this appears to be an issue where you are not calling super.onOptionsItemSelected()
in your Activity. We should add the @CallSuper
annotations to each of the ComponentActivity
Menu APIs to ensure that you actually trigger the default behavior.
gr...@ynab.com <gr...@ynab.com> #5
The documentation for onOptionsItemSelected()
says:
Returns: boolean Return false to allow normal menu processing to proceed, true to consume it here.
Is this no longer true? Is it expected that returning false
should no longer cause the default behaviour to be triggered?
ro...@bbc.co.uk <ro...@bbc.co.uk> #6
I don't understand the Google comments on and repurposing of this issue. We have the same issue as the OP - our fragment's override of onCreateOptionsMenu
is not called and the menu is not populated.
We don't even have an onOptionsItemSelected
implementation in this class.
We are experiencing this after going from AndroidX Fragment 1.4.1 to 1.5.0.
ro...@bbc.co.uk <ro...@bbc.co.uk> #7
I've investigated our instance of this - it's in an old legacy UI not reflective of what we'd do now - and I've found the following.
We have an Activity to which fragments are added via ViewPager
(v1) and FragmentPagerAdapter
.
In the Activity we have:
override fun onCreateOptionsMenu(menu: Menu): Boolean {
this.menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onOptionsItemSelected(menuItem: MenuItem): Boolean {
when (menuItem.itemId) {
android.R.id.home -> {
this.onBackPressed()
return true
}
}
return super.onOptionsItemSelected(menuItem)
}
and in the Fragment(s) we have:
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.search, menu)
val searchMenuItem = menu.findItem(R.id.search)
val searchView = searchMenuItem.actionView as SearchView
searchView.setOnSearchClickListener { view ->
//irrelevant
}
}
In AndroidX Fragment 1.4.1, the Fragment menu's items are appended to the Activity's menu.
In AndroidX Fragment 1.5.0, the Fragment menu's creation is never called, unless we delete the Activity's onCreateOptionsMenu
implementation. If we do this, it is called and populated.
I realise this might be a weird arrangement possibly dependent on undocumented behaviours. We can also make use of this finding since the main menu is not actually required in our problematic case.
Nonetheless, something changed between 1.4.1 and 1.5.0.
ju...@gmail.com <ju...@gmail.com> #8
The warning might be enough to it to make the migration happen successfully.
ap...@google.com <ap...@google.com> #9
Branch: androidx-main
commit e303243b7b3b27281485a333c408d16a4a2ff151
Author: Jeremy Woods <jbwoods@google.com>
Date: Fri Jul 15 15:59:41 2022
Ensure Menu ComponentActivity always pass Menu callbacks
There is an issue where if you use the old Menu APIs without calling
super on an implementation of ComponentActivity, then Fragments will
never get the callbacks.
Instead of override the option menu APIs, we should override the more
encompassing panel menu APIs.
RelNote: "ComponentActivity will now properly dispatch menu calls
without the need to call the super function."
Test: existing tests pass
Bug: 238057118
Change-Id: Ie33c57e900be51ab49abfdbe5c57407f61553167
M activity/activity/src/androidTest/AndroidManifest.xml
M activity/activity/src/main/java/androidx/activity/ComponentActivity.java
M activity/activity/src/androidTest/java/androidx/activity/ComponentActivityMenuTest.kt
jb...@google.com <jb...@google.com> #10
This has been fixed internally to ensure that we respect the old default behavior of not requiring the super call.
This will be included in the Activity 1.5.1
release.
rp...@gmail.com <rp...@gmail.com> #11
Thank you for this, both the comments and the fix - much appreciated. I realise how this fits together now, and the outcome is the ideal one.
na...@google.com <na...@google.com> #12
This bug was linked in a change in the following release(s):
androidx.activity:activity:1.6.0-rc02
an...@gmail.com <an...@gmail.com> #13
A little late to the party here. We are overriding override fun onOptionsItemSelected(item: MenuItem): Boolean {
in the activity in order to have the back arrow (in the toolbar auto generated by nav controller) to behave the same as the back button pressed (eg. swipe). We create and dispatch OnBackPressedCallback
in the Fragment, and then do the following in the activity:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
Timber.e("Does this get called?")
// This is again silly. In order for onNavigationUp to behave as back button pressed, we
// have to override toolbar.
when (item.itemId) {
android.R.id.home -> { ... }
}
}
However, I've noticed that with the upgrade to API 34 onOptionsItemSelected
does not get called anymore, hence the arrow press back and the swipe back behave differently. We don't inflate or set any menu's. We are purely overriding onOptionsItemSelected
, because that was the suggested solution for back arrow in navigation component to behave the same as the swipe back. What is the recommended solution now? Thank you
Description
Version used: 1.5.0
Theme used: -
Devices/Android versions reproduced on: -
When updated the androidx fragment from 1.4.0 to 1.5.0 the onCreateOptionsMenu is not called anymore.
SetHasOptionsMenu(true) is called so the menu shall be enabled and everything is working on 1.4.0. Not sure if the issue is related on activity or fragment but it was introduced on 1.5.0.
However the onPrepareOptionsMenu is still called.
Our application uses the old menu methods heavily so migration to MenuProvider is not feasible.