Home > Blockchain >  Android Searchview gets refocused when returning from another fragment
Android Searchview gets refocused when returning from another fragment

Time:12-31

Currently using jetpack navigation and want to use a single activity. However, the searchview keeps regaining focus whenever I return from the card details back to the search, even though I have removed focus after clicking search. See here.

What I'm currently doing to remove focus is I have a dummy view above my toolbar

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@ id/database_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        style="@style/Widget.MaterialComponents.Toolbar.Primary" />

    <!-- Dummy view for receiving focus after SearchView clears focus -->
    <View
        android:id="@ id/focus_dummy_view"
        android:focusableInTouchMode="true"
        android:focusable="true"
        android:background="@android:color/transparent"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        app:layout_constraintBottom_toTopOf="@id/database_toolbar" />

and after I perform a search, I do

    searchView.clearFocus()
    binding.focusDummyView.requestFocus()

My current workaround is to host the details in an activity, but I'd rather not. I have tried making a new project and the searchview seems to still get focus but the keyboard doesn't flicker?

Some additional context is that this only occured after updating to "androidx.fragment:fragment-ktx:1.4.0" from 1.2.5. I raised this issue in bugtracker but they have not responded to me yet. Wondering if anybody knows a fix?

CodePudding user response:

Why don't you try

In Kotlin

     override fun onResume(){
        super.onResume()
        // Only runs if there is a view that is currently focused
        searchView.currentFocus?.let { view ->
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
        imm?.hideSoftInputFromWindow(view.windowToken, 0)
        searchView.clearFocus()

}
   
        }

CodePudding user response:

You have to add this two lines of code in your root layout xml. To Clear focus from a View you can call getCurrentFocus().clearFocus() But remember that this will Simply move focus from current view to the first element in your view, not Completely removing focus from views; So if the first element in your layout is a EditText it's going to gain focus again.

A way to make sure your view doesn't get focus again is to add this attributes to your root view in your xml, enabling root view to capture focus. To Clear focus from a View you can call getCurrentFocus().clearFocus() But remember that this will Simply move focus from current view to the first element in your view, not Completely removing focus from views; So if the first element in your layout is a EditText it's going to gain focus again.

A way to make sure your view doesn't get focus again is to add this attributes to your root view in your xml, enabling root view to capture focus

android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"

After that you need to clear focus from onResume() I think it will help.

  • Related