Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dealing with Multiple Fragments in a ViewPager #22

Open
irvine752 opened this issue Oct 16, 2021 · 7 comments
Open

Dealing with Multiple Fragments in a ViewPager #22

irvine752 opened this issue Oct 16, 2021 · 7 comments

Comments

@irvine752
Copy link

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?

@mars885
Copy link
Owner

mars885 commented Oct 27, 2021

Hello.

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

Thanks.

@ghost
Copy link

ghost commented Nov 17, 2021

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 👍

@mars885
Copy link
Owner

mars885 commented Nov 17, 2021

@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.

@ghost
Copy link

ghost commented Nov 18, 2021

@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.

@irvine752
Copy link
Author

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.

@ghost
Copy link

ghost commented Nov 26, 2021

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.

@irvine752
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants