Shahzeb
Shahzeb

Reputation: 41

Fragment not loading after second click on bottom navigation

i am trying to add viewpager and tablayout along with bottom navigation. First time fragment loads fine then i click the item the fragment is not loading properly here is my main activity code

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <include layout="@layout/toolbar"
        android:id="@+id/layout_toolbar"/>

    <FrameLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/bottom_nav_menu"
        android:background="@drawable/bg_bottom_bar"
        />

</LinearLayout>

Kotlin Code

    bottomNav.setOnNavigationItemSelectedListener {
            var selectedFragment: Fragment? = null
            when (it.itemId) {
                R.id.homeFragment -> {
                    replaceFragment(fragment = HomeFragment())
                    return@setOnNavigationItemSelectedListener true
                }
                R.id.fixtures -> {
                    replaceFragment(fragment = AllMatchsFragment())
                    return@setOnNavigationItemSelectedListener true
                }


            }

            false
        }
    private fun replaceFragment(fragment: Fragment) {
        supportFragmentManager.beginTransaction().replace(R.id.fragmentContainer, fragment).commit()
    }

Here is fragment code where i want to load viewpager and tablayout:

        homePagerAdapter = HomeViewpagerAdapter(activity!!.supportFragmentManager)
        homePagerAdapter!!.populateFragment(AllMatchsFragment())
        homePagerAdapter!!.populateFragment(LiveMatchesFragment())

        view.homeViewPager.adapter=homePagerAdapter
        view.tabLayout.setupWithViewPager(view.homeViewPager)

        return view

Fragment XML

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="172dp"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabTextColor="@color/colorPrimary"
        app:tabSelectedTextColor="@color/colorAccent"
        >


    </com.google.android.material.tabs.TabLayout>


    <androidx.viewpager.widget.ViewPager
        android:id="@+id/homeViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tabLayout"/>

ViewPagerAdapter

    val tabNameList= listOf("All","Live")
    val fragmentList = mutableListOf<Fragment>()
    override fun getItem(position: Int): Fragment {
        return fragmentList[position]
    }
    override fun getCount(): Int {
        return 2
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return tabNameList[position]
    }

    fun populateFragment(fragment: Fragment){
        fragmentList.add(fragment)

    }

When I perform second click on the second viewpager got stuck and fragment disappear when I close and reopen the app it works fine and after second click again remain the same.

Upvotes: 1

Views: 860

Answers (1)

user12251581
user12251581

Reputation:

Try to use childFragmentManager instead of supportFragmentManager

homePagerAdapter = HomeViewpagerAdapter(activity!!.supportFragmentManager)

Upvotes: 1

Related Questions