'ViewPager with Bottom Navigation Wont Call onViewCreated Method Until select a Bottom Tab
Sorry for the complicated question I don't know how to ask that
I have a Bottom Tabbar set to index 3 when the app first launch
and in index 2 of the BottomBar there is a Fragment and inside that Fragment is a ViewPager with TabLayout and two other Fragments and everything is working fine but there is an issue that there is data coming from database to the TabLayout Fragments and should call onViewCreated when app launch but it won't call unless i select the tabbar that holds the ViewPager and TabLayout and that is index 2 why onViewCreated Wont't Call unlese I select the tab
How did i test this and come to this conclusion i have added Log.e("called", "fromFragment....") to the BottomBar fragment index(2) onViewCrated and it printed out in logcat in first Launches the app
but in the two fragments that's with tabLayout and viewPager it will print after i select the BottomBar tab
and i discover later that even the fragments won't attached also until i select the tab
tabBar Index(3) Fragment layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".fragment.OrdersFragment">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextAppearance="@style/TabLayoutStyle"
app:tabBackground="@color/white"
android:elevation="4dp"
app:tabGravity="fill"
app:tabIndicatorColor="@color/del_color"
app:tabTextColor="@color/dark"
tools:ignore="UnusedAttribute" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroundGray"
android:layout_below="@+id/tablayout"/>
</RelativeLayout>
here is the fragment that have the ViewPager
class OrdersFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_orders, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setUpTabs()
}
private fun setUpTabs() {
val adapter = viewPagerAdapter(childFragmentManager)
adapter.addFragment(DriverFragment(), "Delivary")
adapter.addFragment(UserOrdersFragment(), "Orders" )
adapter.notifyDataSetChanged()
val VP = view?.findViewById<ViewPager>(R.id.viewPager)
VP?.adapter = adapter
tablayout.setupWithViewPager(VP, true)
VP?.currentItem = 1
tablayout.getTabAt(0)
}
}
//FragmentStatePagerAdapter
class viewPagerAdapter(supportFragmentManager: FragmentManager): FragmentPagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT){
val fragmentList = ArrayList<Fragment>()
val fragmentTitleList = ArrayList<String>()
override fun getCount(): Int {
return fragmentList.size
}
override fun getItem(position: Int): Fragment {
return fragmentList[position]
}
override fun getPageTitle(position: Int): CharSequence? {
return fragmentTitleList[position]
}
fun addFragment(fragment: Fragment, title: String){
fragmentList.add(fragment)
fragmentTitleList.add(title)
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

