persistent-search-view icon indicating copy to clipboard operation
persistent-search-view copied to clipboard

Dealing with Multiple Fragments in a ViewPager

Open irvine752 opened this issue 4 years ago • 7 comments

I have a couple of fragments in a viewPager2. Two of the fragments have persistent search views. When I toggle back & forth between the fragments, tapping to activate the search query does not work.

I was thinking it's related to the Lifecycle of the fragments which toggles between pause & resume depending on which fragment is active. Do we need to do anything special when multiple instances of the persistent search views are active at a time?

irvine752 avatar Oct 16 '21 21:10 irvine752

Hello.

Can you set up a sample repo project and send me a link so that I could take a look?

Thanks.

mars885 avatar Oct 27 '21 21:10 mars885

I've run into same issue @irvine752 has described: Basically I have a ViewPager, PersistentSearchView is only in first fragment. When staying in first fragment works good, but as soon as I swipe right onto the second fragment, if I swipe back left PersistentSearchView does not work anymore, it simply won't do anything.

I solved this way: In [onResume] method of Fragment which contains PersistentSearchView:

override fun onResume() {
    super.onResume()
    //Reinitialize PersistentSearchView 
    persistentSearchView = findViewbyId(R.id.xxx)
    
    //Set OnSearch Confirmed Listener once again
    persistentSearchView.setOnSearchConfirmedListener((object : OnSearchConfirmedListener{
        override fun onSearchConfirmed(searchView: PersistentSearchView, query: String) {
            //Do your stuff
        }
    }
    
    //Set On Clear Input Click Listener once again
    txtBlinkSearch.setOnClearInputBtnClickListener{
         //Do your stuff
    }

}

This way, it doesn't matter how many times we swipe right or left, PersistentSearchView will always work :) Hope this will be useful to anyone who run into the same issue.

Thank you @mars885 for this library, you saved me a lot of time 👍

ghost avatar Nov 17 '21 07:11 ghost

@artemius30 Thanks for sharing your solution. However, it's more like a workaround and the behavior is definitely not right. I suspect that there might be some issue with loosing focus over the PersistentSearchView.

If someone can set up a reproducible repo and send me a link to it so that I could take a look when I have time, I'll be extremely thankful.

mars885 avatar Nov 17 '21 20:11 mars885

@mars885 Thank you for your help. I managed to develop a very simple application which shows you the issue.

Since it's in a public repository, you should be able to import it via:

git clone https://github.com/artemius30/TestSearchView.git

Basically, it's made of a ViewPager which contains 2 Fragments. If we search before swiping Fragment everything works, but if we swipe right and then back, searching is impossible, since PersistentSearchView won't do anything.

Hope this will help you understand the issue :) If you need anything, I'm here.

Thank you again, hope to hear from you soon.

ghost avatar Nov 18 '21 15:11 ghost

Thanks for the update on this issue. @artemius30 saw the same issue as I did. Do we have any update on a potential fix so far? For now, I will update my code to use the fix mentioned above.

I was also wondering if there's a way to customize or remove the speech to text Google warning.

irvine752 avatar Nov 24 '21 17:11 irvine752

Sorry @irvine752 . I haven't heard from anyone about this... You might try to use fix I mentioned, but keep in mind, as @mars885 said, even if it works, it's just a workaround. Not a good thing to keep in production environments, that's for sure.

Can't help you with Speech to Text functionality, didn't had chance to try it.

ghost avatar Nov 26 '21 16:11 ghost

@mars885 Any updates so far? @artemius30 Can the patch be used with ViewBinding? I'm not sure if it's possible to reinitialize the SearchView.

irvine752 avatar Dec 12 '21 04:12 irvine752