Не правильно работает возврат к предыдущему фрагменту Navigation Component, при использовании TabLayout и ViewPager
Пишу небольшое приложение чтобы разобраться в основах Navigation Component, создал граф, создал пару фрагментов, сделал переход от одного к другому и передачу аргументов все работает. Решил попробовать добавить на первый фрагмент TabLayout и ViewPager все работает, только вот теперь при возврате с второго фрагмента на первый, там пусто и не один из фрагментов в ViewPager не погружается а TabLayout подвисает. Подскажите пожалуйста в чем может быть дело и как поправить? Может я вообще не так подхожу к этой задаче, не совсем понимаю как правильно реализовывать схему с TabLayout и ViewPager в Navigation Component, посоветуйте пожалуйста как правильно?!
MainActivity
var navController: NavController? = null
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navController = Navigation.findNavController(this, R.id.nav_host_fragment)
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph"
tools:layout="@layout/fragment_main" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainFragment
class MainFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_main, container, false)
val tab_layout = view.findViewById(R.id.tab_layout) as TabLayout
val pager = view.findViewById(R.id.pager) as ViewPager
initTab(tab_layout, pager)
return view
}
private fun initTab(tab_layout: TabLayout, pager: ViewPager) {
tab_layout.addTab(tab_layout.newTab().setIcon(R.drawable.ic_launcher_background))
tab_layout.addTab(tab_layout.newTab().setIcon(R.drawable.ic_launcher_background))
tab_layout.addTab(tab_layout.newTab().setIcon(R.drawable.ic_launcher_background))
tab_layout.addTab(tab_layout.newTab().setIcon(R.drawable.ic_launcher_background))
tab_layout.tabGravity = TabLayout.GRAVITY_FILL
val adapter = MyPagerAdapter(activity!!.supportFragmentManager, tab_layout.tabCount)
pager.adapter = adapter
pager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tab_layout))
tab_layout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab) {
pager.currentItem = tab.position
}
override fun onTabUnselected(tab: TabLayout.Tab) {
}
override fun onTabReselected(tab: TabLayout.Tab) {
}
})
}
}
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.viewpager.widget.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/tab_layout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
ForecastFragment
class ForecastFragment() : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_forecast, container, false)
val btn = view.findViewById(R.id.btn) as Button
val bundle = Bundle().apply {
putString("Hello", "Hello")
}
btn.setOnClickListener {
navController?.navigate(R.id.detailForecastFragment, bundle)
}
return view
}
}
fragment_forecast.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".ForecastFragment">
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
DetailForecastFragment
class DetailForecastFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_detail_forecast, container, false)
val detail_forecast = view.findViewById(R.id.detail_forecast) as TextView
detail_forecast.text = arguments?.getString("Hello")
return view
}
}
MyPagerAdapter
class MyPagerAdapter(fm: FragmentManager, internal var numOfTabs: Int) : FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
override fun getItem(position: Int): Fragment {
when (position) {
0 -> {
return ForecastFragment()
}
1 -> {
return BlankFragment()
}
2 -> {
return BlankFragment2()
}
else -> {
return BlankFragment3()
}
}
}
override fun getCount(): Int {
return numOfTabs
}
}
nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_graph"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/forecastFragment"
android:name="com.example.weatherforecastapp.ForecastFragment"
android:label="fragment_forecast"
tools:layout="@layout/fragment_forecast">
<action
android:id="@+id/action_mainFragment2_to_detailForecastFragment"
app:destination="@id/detailForecastFragment"
app:popUpTo="@id/detailForecastFragment"/>
</fragment>
<fragment
android:id="@+id/detailForecastFragment"
android:name="com.example.weatherforecastapp.DetailForecastFragment"
android:label="fragment_detail_forecast"
tools:layout="@layout/fragment_detail_forecast" />
<fragment
android:id="@+id/mainFragment"
android:name="com.example.weatherforecastapp.MainFragment"
android:label="MainFragment" />
</navigation>