0

I have an Activity "A" with a ViewPager (with TabLayout) inside. The viewPager includes 2 fragments "X" and "Y", since ViewPager handles the lifecycle of the fragments. When the activity is on resume, the fragments also go to resume.

When I launch the activity initially, the first tab - "X" fragment is in focus and displays the UI, during this I am getting a soft keyboard. (This keyboard is launched by spawning a mainUIThread with a delay of 1.5secs during the onResume of the "Y" fragment).

The question is, why does the onResume of "Y" fragment interfere while the "X" fragment's tab during onResume ? Is there a way to avoid it ?

I want to show the keyboard only for the "Y" fragment and not for the "X" fragment. Since the "Y" frament's onResume handles the keyboard somehow the keyboard gets visible.

Angus
  • 12,133
  • 29
  • 96
  • 151
  • 1
    You don't say what library/version of Viewpager you are using e.g. support v4, androidx, etc In older viewpagers any fragment +/- the off screen limit were brought to `onResume` even if they were not displayed. In Androidx the `BEHAVIOR_SET_USER_VISIBLE_HINT` behaviour that causes this was replace with `BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT` where only the visible fragment is resumed. You also don't show any code on how you create the viewpager. – Andrew Apr 17 '20 at 10:02
  • @Andrew : The Viewpager uses the androidx library. – Angus Apr 21 '20 at 07:03

2 Answers2

1

Both fragment onResume should be standalone and shouldn't affect the other one, but if this is happening, I can suggest you not to call fragment's on resume directly, instead use activity's onresume, there check for active fragment and, create resumeMethod which had all the things you need inside each fragment's onresume, and call active fragment's on resume, like fragmentA.resumeMethod() if fragment A is active, in this way you might avoid your error.

Ashish Sharma
  • 566
  • 5
  • 20
  • Thank you. onResume is there only in Y Fragment. X Fragment and the Activity doesnt have onresume method. Do you want me to create onResume in the activity and call the onResume of the fragment only if its active ? I think the viewPager instantiates both the fragments. And that way both would be active – Angus Apr 17 '20 at 04:57
  • @Angus check for current page, if current page is Y then, onResume of Y should be called inside activity's OnResume – Ashish Sharma Apr 17 '20 at 05:01
  • @Override public void onResume(){ super.onResume(); if (currentPage == TAB_POSITION) { final Fragment resumedFragment = kViewPagerAdapter.getFragmentAtPosition(currentPage); resumedFragment.onResume(); } else { KeyUtils.hideKeyboard(getCurrentFocus()); } } – Angus Apr 17 '20 at 07:25
  • I made the above code change in the onResume fuction.. But still no difference. – Angus Apr 17 '20 at 07:26
1

How about using OnPageChangeListener?

You can show the keyboard when Y fragment is selected, and hide the keyboard when other pages are selected.

my_view_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
    override fun onPageSelected(position: Int) {
        if (position == yFragmentPosition) {
            showKeyboard()
        } else {
            hideKeyboard()
        }
    }
    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
    override fun onPageScrollStateChanged(state: Int) {}
})
MiJey
  • 21
  • 3
  • Thank you. I have similar logic in place as above. But the issue is Y fragment has onResume(), which calls show keyboard with a delay of 1.5sec (runOnMainThread). The X fragment doesnt have onResume() method and the Activity either. The Y fragments onResume is called somehow, when the X fragments tab was selected. Is there a way to not call onResume of Y Fragment, when X fragment's tab is in selection. – Angus Apr 17 '20 at 04:52
  • 1
    Because ViewPager calls other fragments in advance, onResume of Y fragment can be called in advance. I think it would be better to put the code somewhere(for example, OnPageChangeListener) other than onResume. Please refer to this: [link](https://stackoverflow.com/questions/10073214/viewpager-setoffscreenpagelimit0-doesnt-work-as-expected) – MiJey Apr 17 '20 at 05:17