material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

[SearchView] When using android:enableOnBackInvokedCallback="true" , the SearchView is collapsed together with the keyboard, instead of just the keyboard

Open AndroidDeveloperLB opened this issue 1 month ago • 10 comments

Description: Full description of issue here I have a toolbar with a search menu item in it. I also use setOnActionExpandListener on it to handle some stuff, related to predictive back gesture . As such, I enable the predictive back gesture in the manifest:

  android:enableOnBackInvokedCallback="true" 

All works fine everywhere else, except here. When I put a focus on the SearchView and I enter something, and then press back key, it closes both the keyboard AND the Search View, making it impossible to go back to it and change it, and of course to see the results in case the search was performed. When the above is set to false, it works fine.

Expected behavior: Screenshots and/or description of expected behavior Should close only the keyboard first, and only if I press back key again, it should close the SearchView.

Source code: The code snippet which is causing this issue

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        menu.findItem(R.id.menuItem_search)
            .setOnActionExpandListener(object : OnActionExpandListener {
                override fun onMenuItemActionExpand(item: MenuItem): Boolean {
                    Log.d("AppLog", "onMenuItemActionExpand")
                    return true
                }

                override fun onMenuItemActionCollapse(item: MenuItem): Boolean {
                    Log.d("AppLog", "onMenuItemActionCollapse")
                    return true
                }
            })
        return true
    }
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.lb.myapplication.MainActivity">

    <item
        android:id="@+id/menuItem_search"
        android:icon="@android:drawable/ic_search_category_default"
        android:title="search"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        app:showAsAction="always|collapseActionView" />

    <item
        android:id="@+id/action_settings" android:orderInCategory="100"
        android:title="@string/action_settings" app:showAsAction="never" />
</menu>

Minimal sample app repro: Please consider attaching a minimal sample app that reproduces the issue. This will help narrow down the conditions required for reproducing the issue, and it will speed up the bug fix process. You may attach a zip file of the sample app or link to a GitHub repo that contains the sample app.

My Application.zip

Video:

studio64_2024-05-21_14-21-38.zip

Android API version: Android API version here 34

Material Library version: Material Android Library version you are using here (e.g., 1.1.0-alpha07)

implementation 'com.google.android.material:material:1.13.0-alpha02'

Device: Device on which the bug was encountered here Pixel 6

I can see a similar behavior on the sample of the repository, but here it's not in the toolbar at all, and it's also inconsistent. Watch:

scrcpy_2024-05-21_14-31-57.zip

I've also reported about this here but nobody has fixed it yet, and even my sample video and sample project were deleted by the forum: https://issuetracker.google.com/issues/326681254

AndroidDeveloperLB avatar May 21 '24 11:05 AndroidDeveloperLB