-1

I'm using a ViewPager to slide between different fragments as tabs. I noticed that just by creating the adapter, the onCreateView function inside each fragment is being called immediately, which is no good since I would like to run the code inside these functions at a specific given time.

My code in MainActivity:

private var profileTab: TabLayout.Tab? = null
    private var swipeTab: TabLayout.Tab? = null
    private var matchesTab: TabLayout.Tab? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        profileTab = navigationTabs.newTab()
        swipeTab = navigationTabs.newTab()
        matchesTab = navigationTabs.newTab()

        navigationTabs.addTab(profileTab!!)
        navigationTabs.addTab(swipeTab!!)
        navigationTabs.addTab(matchesTab!!)

        val adapter = PagerAdapter(supportFragmentManager, navigationTabs.tabCount)
        fragmentContainer.adapter = adapter

My code of PagerAdapter.kt:

class PagerAdapter (fm: FragmentManager, private val mNoOfTabs: Int) : FragmentStatePagerAdapter(fm, mNoOfTabs) {

    override fun getItem(position: Int): Fragment {
        when (position) {
            0 -> {
                return ProfileFragment()
            }
            1 -> {
                return SwipeFragment()
            }
            2 -> {
                return MatchesFragment()
            }
        }
        return null!!
    }

    override fun getCount() = mNoOfTabs
}

And in activity_main.xml (if it matters):

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/navigationTabs"
        android:layout_width="match_parent"
        android:layout_height="@dimen/navigation_height"
        android:background="@drawable/navigation_shadow"
        app:layout_constraintTop_toTopOf="parent"
        app:tabIndicator="@null"
        app:tabMinWidth="@dimen/navigation_height"
        app:tabRippleColor="@null" />

<!--    // the fragment will be displayed here:-->
    <androidx.viewpager.widget.ViewPager
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/navigationTabs"/>

Is this the default behavior? Can it be prevented, so that the fragments won't be initiated immediately?

sir-haver
  • 3,096
  • 7
  • 41
  • 85

2 Answers2

1

ViewPager will initialize two fragments minimally. ViewPager allows us to set setOffscreenPageLimit(), but it require at least 1, so it will initialize first and next fragment. Read more here ViewPager.setOffscreenPageLimit(0) doesn't work as expected

Try to invoke code in fragments by setting onPageChangedListener() in MainActivity, so you can execute code when user swiped/opened fragment.

Anton Sarmatin
  • 505
  • 2
  • 8
  • 1
    Thank you very much I would actually prefer to invoke the code there in the background but that is for another new question thanks – sir-haver Dec 23 '19 at 09:20
1

ViewPager in Android by default, loads 2 more fragments with current fragment. That is one previous fragment and one next fragment for smooth animation. And you can not alter this default functionality.

One hack to achieve what you want is to use setUserVisibleHint method of fragment. this will called whenever fragments gets visible. But I am not recommending you to use this method

Bhargav Thanki
  • 4,924
  • 2
  • 37
  • 43