This is my profile fragment where I am using a viewpager :
@Suppress("DEPRECATION")
class ProfileFragment : Fragment() {
private lateinit var viewPagerAdaptor: ViewPagerAdaptor
private val binding: FragmentProfileBinding by lazy {
FragmentProfileBinding.inflate(layoutInflater)
}
private lateinit var currentUserId: String
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// User id
currentUserId = FirebaseAuth.getInstance().currentUser!!.uid
// Edit profile button
binding.editProfileButton.setOnClickListener{
startActivity(
Intent(requireActivity(), EditProfileActivity::class.java)
.putExtra("userId", currentUserId)
.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
)
}
// add post button
binding.addPostButton.setOnClickListener{
startActivity(Intent(requireActivity(), AddPostActivity::class.java))
}
// Settings Button
binding.settingsButton.setOnClickListener {
startActivity(Intent(requireActivity(), SettingsActivity::class.java))
}
return binding.root
}
@SuppressLint("SetTextI18n")
private fun init() {
// Setup tabs
setUpTabs()
}
/*
* set up tab layout : My posts | My Incubates | My Bookmarks
* */
private fun setUpTabs() {
viewPagerAdaptor = ViewPagerAdaptor(requireActivity().supportFragmentManager)
viewPagerAdaptor.addFragments(MyPostFragment())
viewPagerAdaptor.addFragments(MyIncubatesFragment())
viewPagerAdaptor.addFragments(MyBookmarksFragment())
binding.profileViewPager.adapter = viewPagerAdaptor
binding.profileTabLayout.setupWithViewPager(binding.profileViewPager)
binding.profileTabLayout.getTabAt(0)!!.setIcon(R.drawable.grid_view)
binding.profileTabLayout.getTabAt(1)!!.setIcon(R.drawable.video_play_outlined)
binding.profileTabLayout.getTabAt(2)!!.setIcon(R.drawable.bookmark)
}
}
There is lifecycle issues with this implementation, viewpager inside a fragment is not working fine with my above implementation.
I expect the profile fragment to come to the previous state, when moving from the another activity to this or onBackPressed.
Please help me solve this. (following is the stack trace)
2023-12-11 10:26:46.327 1311-18277 AwareLog system_server W AwareMem_Reclaim: generateCompressList procsGroups error!
2023-12-11 10:26:46.698 1311-1355 ZrHung.App...rentWindow system_server D handleMessage CHECK_TRANS_WINDOW_ERROR_MSG
2023-12-11 10:26:50.796 1311-18277 AwareLog system_server W AwareMem_Reclaim: generateCompressList procsGroups error!
2023-12-11 10:26:52.875 1311-1947 ZrHung.App...ocusWindow system_server D FocusWindowErrorScene CheckFreezeScreen
2023-12-11 10:26:53.072 1311-1609 InputDispatcher system_server W channel '5fd2c33 com.bishal.incubator/com.bishal.incubator.profile.EditProfileActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9
2023-12-11 10:26:53.074 1311-1609 InputDispatcher system_server W channel '8a81dad com.bishal.incubator/com.bishal.incubator.home.HomeActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9
2023-12-11 10:26:53.074 1311-1609 InputDispatcher system_server W channel 'a489a9a com.bishal.incubator/com.bishal.incubator.add_post.AddPostActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9
2023-12-11 10:26:53.074 1311-1609 InputDispatcher system_server W channel '1def340 com.bishal.incubator/com.bishal.incubator.home.HomeActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9
---------------------------- PROCESS ENDED (18502) for package com.bishal.incubator ----------------------------
2023-12-11 10:26:53.303 18181-19220 DropBoxUtil com.google.android.gms.persistent E [AppErrors] null InputStream [CONTEXT service_id=254 ]
java.io.IOException: null InputStream
at cgon.d(:com.google.android.gms@234523022@23.45.23 (100400-587848529):209)
at cgmv.l(:com.google.android.gms@234523022@23.45.23 (100400-587848529):7)
at cgmp.apply(:com.google.android.gms@234523022@23.45.23 (100400-587848529):37)
at rlc.apply(:com.google.android.gms@234523022@23.45.23 (100400-587848529):5)
at rlo.apply(:com.google.android.gms@234523022@23.45.23 (100400-587848529):45)
at cyxp.a(:com.google.android.gms@234523022@23.45.23 (100400-587848529):3)
at dhiq.a(:com.google.android.gms@234523022@23.45.23 (100400-587848529):13)
at dnvc.d(:com.google.android.gms@234523022@23.45.23 (100400-587848529):3)
at dnve.run(:com.google.android.gms@234523022@23.45.23 (100400-587848529):42)
at dnwp.execute(:com.google.android.gms@234523022@23.45.23 (100400-587848529):1)
at dnuy.h(:com.google.android.gms@234523022@23.45.23 (100400-587848529):1)
at dnuy.hi(:com.google.android.gms@234523022@23.45.23 (100400-587848529):40)
at dnve.g(:com.google.android.gms@234523022@23.45.23 (100400-587848529):13)
at cyxf.a(:com.google.android.gms@234523022@23.45.23 (100400-587848529):20)
at dhio.a(:com.google.android.gms@234523022@23.45.23 (100400-587848529):13)
at dnwv.a(:com.google.android.gms@234523022@23.45.23 (100400-587848529):20)
at dnyy.a(:com.google.android.gms@234523022@23.45.23 (100400-587848529):3)
at dnxz.run(:com.google.android.gms@234523022@23.45.23 (100400-587848529):21)
at dnza.run(:com.google.android.gms@234523022@23.45.23 (100400-587848529):5)
at dnwx.run(:com.google.android.gms@234523022@23.45.23 (100400-587848529):53)
at dnwp.execute(:com.google.android.gms@234523022@23.45.23 (100400-587848529):1)
at dnwx.execute(:com.google.android.gms@234523022@23.45.23 (100400-587848529):76)
at dnuy.h(:com.google.android.gms@234523022@23.45.23 (100400-587848529):1)
at dnuy.hi(:com.google.android.gms@234523022@23.45.23 (100400-587848529):40)
at dnwz.c(:com.google.android.gms@234523022@23.45.23 (100400-587848529):31)
at cyxj.i(:com.google.android.gms@234523022@23.45.23 (100400-587848529):18)
at cyxo.a(:com.google.android.gms@234523022@23.45.23 (100400-587848529):10)
at dhiq.a(:com.google.android.gms@234523022@23.45.23 (100400-587848529):13)
at dnvc.d(:com.google.android.gms@234523022@23.45.23 (100400-587848529):3)
at dnve.run(:com.google.android.gms@234523022@23.45.23 (100400-587848529):42)
at dnwp.execute(:com.google.android.gms@234523022@23.45.23 (100400-587848529):1)
at dnuy.h(:com.google.android.gms@234523022@23.45.23 (100400-587848529):1)
at dnuy.hi(:com.google.android.gms@234523022@23.45.23 (100400-587848529):40)
at dnve.g(:com.google.android.gms@234523022@23.45.23 (100400-587848529):13)
at cyxr.a(:com.google.android.gms@234523022@23.45.23 (100400-587848529):20)
at dhio.a(:com.google.android.gms@234523022@23.45.23 (100400-587848529):13)
at dnwv.a(:com.google.android.gms@234523022@23.45.23 (100400-587848529):20)
at dnyy.a(:com.google.android.gms@234523022@23.45.23 (100400-587848529):3)
at dnxz.run(:com.google.android.gms@234523022@23.45.23 (100400-587848529):21)
at dnza.run(:com.google.android.gms@234523022@23.45.23 (100400-587848529):5)
at dnwx.run(:com.google.android.gms@234523022@23.45.23 (100400-587848529):53)
at dnwp.execute(:com.google.android.gms@234523022@23.45.23 (100400-587848529):1)
at dnwx.execute(:com.google.android.gms@234523022@23.45.23 (100400-587848529):76)
at dnuy.h(:com.google.android.gms@234523022@23.45.23 (100400-587848529):1)
at dnuy.hi(:com.google.android.gms@234523022@23.45.23 (100400-587848529):40)
at dnwz.c(:com.google.android.gms@234523022@23.45.23 (100400-587848529):31)
at cyxy.c(:com.google.android.gms@234523022@23.45.23 (100400-587848529):56)
at cyxy.b(:com.google.android.gms@234523022@23.45.23 (100400-587848529):10)
at com.google.android.gms.stats.service.DropBoxEntryAddedChimeraService.a(:com.google.android.gms@234523022@23.45.23 (100400-587848529):375)
---------------------------- PROCESS STARTED (19232) for package com.bishal.incubator ----------------------------
The solution is to use (under the setUpTabs()
):
viewPagerAdaptor = ViewPagerAdaptor(childFragmentManager)
Instead of :
viewPagerAdaptor = ViewPagerAdaptor(requireActivity().supportFragmentManager)
Below is the ViewPagerAdaptor class :
class ViewPagerAdaptor (fragmentManager: FragmentManager) :
FragmentPagerAdapter(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
private val fragmentList = mutableListOf<Fragment>()
override fun getCount(): Int {
return fragmentList.size
}
override fun getItem(position: Int): Fragment {
return fragmentList[position]
}
fun addFragments(fragment: Fragment) {
fragmentList.add(fragment)
}
}