2

I have a SearchView in the Toolbar of an Activity, which hosts a Fragment which has a ViewPager of 4 tabs, each tab consisting of another Fragment (there is a reason for this structure so I don't want to dwell on it unless someone thinks this is the reason for my problem). When the user originally searches something, everything works fine. However, when the user goes and edits the search query, and hits enter, the tab that was already selected doesn't refresh (it will refresh when we navigate away from it to other tabs and then come back).

The following are screenshots of what I mean (the first image is after the original search where everything is all and well, the second image is me editing the query, the third image is after I hit enter - the results stay the same and nothing is updated since isVisible() in the code below returns false when the fragment to me seems clearly visible).

enter image description here enter image description here enter image description here

When a query is submitted essentially what happens is the activity sends the search query to the fragment that contains the ViewPager and TabLayout, which passes it to the current Tab. This is the code that is called in the Fragment that contains the ViewPager after the query is submitted:

public void setSearchQuery (String query) {
        //mTabLayout.requestFocus();
        mSearchQuery = query;

        Fragment fragment = mPagerAdapter.getItem(mViewPager.getCurrentItem()); // should theoretically get the current fragment
        mTabLayout.getTabAt(mViewPager.getCurrentItem()).select();

        if (fragment != null && fragment.isVisible()) {
            ((ResultsPagerAdapter.QuerySubmitCallback) fragment).submitQuery(query);
        }
    }

The submitQuery() line at the bottom is never called because isVisible returns false. However, when logging the lifecycle methods of the current tab the last calls are onStart() followed by onResume(). When I click on the toolbar and edit the query and hit enter, this doesn't change, and the current tab is visible, so I have no idea why this returns false.

I should add in the hosting Activity after the user submits their query, I remove focus from the Toolbar and SearchView so that the keyboard collapses after the search is entered.

sbearben
  • 323
  • 6
  • 16

1 Answers1

0

When you switch the tabs,the fragment will call setUserVisibleHint(boolean isVisibleToUser).And then,you can get the value of isVisibleToUser by getUserVisibleHint().

If you want to get isVisible of all kinds of fragment,you should update the value of isVisible in such methods:

onAttach and onDetach()

onStart() and onStop()

onHiddenChanged

setUserVisibleHint

onViewAttachedToWindow and onViewDetachedFromWindow

Motee
  • 112
  • 7
  • Thank you for your reply. This actually ended up being an issue with me not getting the correct fragment from the pager. When calling mPagerAdapter.getItem() it was actually creating a new fragment at the selected index, as opposed to returning the current one (this is my second time having an issue getting the current fragment from viewpager - not sure why this is such a difficult thing to do). I ended up using the solution found here: https://stackoverflow.com/a/18611036/7648952 - which to me seems very ugly but I guess will do as I don't see another way around this. – sbearben Aug 09 '18 at 01:09
  • Okay,If you want solve the problem instead of change another solution,you should show more code about ```mPagerAdapter```. And that's my code of ```mPagerAdapter.getItem``` private List mFragments; @Override public Fragment getItem(int position) { // TODO Auto-generated method stub return mFragments.get(position); } – Motee Aug 09 '18 at 01:35