I am using a ViewPager having 2 pages. First page of that ViewPager is MainFragment. MainFragment has ViewPager as bottomNavigationView. Each page has a FragmentContainerView and By default they contains HomeFragment, CommunityFragment and NotificationFragment respectively. Now if i am in HomeFragment and I click on a profile button, which is there in the HomeFragment so It transact to ProfileFragment and from there setting and so on. And on clicking on back button it get back perfectly one-by-one. But it do not happens same with other FragmentContainerView. Even they get back directly to the parent fragment. Overall i am unable to handle the backstack between different ViewPager and fragments.
This is FragmentContainer of HomeFragment or the first page of the MainFragment's ViewPager in HomeFragmentContainer.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=".fragments.HomeFragmentContainer">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container_home_id"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
And in the HomeFragmentContainer.kt file
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if(savedInstanceState == null) { // initial transaction should be wrapped like this
childFragmentManager.beginTransaction()
.replace(R.id.fragment_container_home_id, HomeFragment())
.commitAllowingStateLoss()
}
}
This is a simple transaction between fragments (HomeFragment to ProfileFragment)
activity?.supportFragmentManager?.beginTransaction()?.add(R.id.fragment_container_home_id, ProfileFragment())?.addToBackStack(null)?.commit()
It is ok till here But if i am in other fragments like NotificationContainerFragment or other then it's so tough to handle it. Because there are some fragments like ProfileFragment and PostFragment have to call from every fragment. So here to handle the confusion of FragmentContainers i transact like this
val containerId = (view?.parent as ViewGroup).id
activity?.supportFragmentManager?.beginTransaction()?.add(containerId, profileFragment)?.addToBackStack(null)?.commit()
Now the handling of BackPressed() in MainActivity is here
if (view_pager_main_activity?.currentItem == 0)
{
if (view_pager_main_fragment?.currentItem == 0)
{
val recyclerView = findViewById<RecyclerView>(R.id.recycler_view_home)
val appbarHome = findViewById<AppBarLayout>(R.id.appbar_home)
val layoutManager = recyclerView?.layoutManager as LinearLayoutManager
when {
layoutManager.findFirstCompletelyVisibleItemPosition() == 0 -> {
super.onBackPressed()
}
supportFragmentManager.backStackEntryCount != 0 -> {
supportFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
}
else -> {
layoutManager.scrollToPositionWithOffset(0, 0)
appbarHome.setExpanded(true)
//recyclerView.smoothScrollToPosition(0)
}
}
}
else
{
// Otherwise, select the previous step.
view_pager_main_fragment?.setCurrentItem(view_pager_main_fragment.currentItem - 1, false)
}
}
else
{
// Otherwise, select the previous step.
view_pager_main_activity?.currentItem = view_pager_main_activity.currentItem - 1
}
from How to handle backstack between fragments in multiple containers?
No comments:
Post a Comment