material-components / material-components/material-components-android

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

Open
#4,185 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Widget: Search
Dominant language
Java
Stars
17.4k
Forks
3.2k
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the attached minimal sample app on Android API 34 with android:enableOnBackInvokedCallback enabled, then reproduce the back-button behavior in the SearchView. Trace the interaction between the SearchView, the action expansion listener, and the keyboard; done means the first back action hides only the keyboard and the second collapses the SearchView.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.