I have several fragments, when the user has completed the onboarding stage he will be redirected to the MainActivity page. But when I want to enter the MainActivity page, the onboarding fragment appears for a few moments. How to fix it?
My launcher_nav.xml (Navigation)
<?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/launcher_nav"
app:startDestination="@id/splashScreenFragment">
<fragment
android:id="@+id/splashScreenFragment"
android:name="com.ryz.nutrilicious.presentation.splashscreen.SplashScreenFragment"
android:label="fragment_splash_screen"
tools:layout="@layout/fragment_splash_screen" >
<action
android:id="@+id/action_splashScreenFragment_to_onBoardingFragment"
app:destination="@id/onBoardingFragment"
app:enterAnim="@android:anim/fade_in"
app:exitAnim="@android:anim/fade_out"
app:popEnterAnim="@android:anim/fade_in"
app:popExitAnim="@android:anim/fade_out"
app:popUpTo="@id/splashScreenFragment"
app:popUpToInclusive="true" />
<action
android:id="@+id/action_splashScreenFragment_to_mainActivity"
app:destination="@id/mainActivity"
app:enterAnim="@android:anim/fade_in"
app:exitAnim="@android:anim/fade_out"
app:popEnterAnim="@android:anim/fade_in"
app:popExitAnim="@android:anim/fade_out"
app:popUpTo="@id/splashScreenFragment"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/onBoardingFragment"
android:name="com.ryz.nutrilicious.presentation.onboarding.OnBoardingFragment"
android:label="fragment_on_boarding"
tools:layout="@layout/fragment_on_boarding" >
<action
android:id="@+id/action_onBoardingFragment_to_introFragment"
app:destination="@id/introFragment"
app:enterAnim="@android:anim/fade_in"
app:exitAnim="@android:anim/fade_out"
app:popEnterAnim="@android:anim/fade_in"
app:popExitAnim="@android:anim/fade_out"
app:popUpToInclusive="true" />
</fragment>
<activity
android:id="@+id/mainActivity"
android:name="com.ryz.nutrilicious.presentation.MainActivity"
android:label="activity_main"
tools:layout="@layout/activity_main" />
<fragment
android:id="@+id/introFragment"
android:name="com.ryz.nutrilicious.presentation.onboarding.IntroFragment"
android:label="fragment_intro"
tools:layout="@layout/fragment_intro" >
<action
android:id="@+id/action_introFragment_to_mainActivity"
app:destination="@id/mainActivity"
app:enterAnim="@android:anim/fade_in"
app:exitAnim="@android:anim/fade_out"
app:popEnterAnim="@android:anim/fade_in"
app:popExitAnim="@android:anim/fade_out"
app:popUpTo="@id/launcher_nav"
app:popUpToInclusive="true" />
</fragment>
</navigation>
IntroFragment.kt
class IntroFragment : Fragment() {
private var _binding: FragmentIntroBinding? = null
private val binding get() = _binding
private val onBoardingViewModel: OnBoardingViewModel by viewModel()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentIntroBinding.inflate(inflater, container, false)
return binding?.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding?.apply {
btnLetsStarted.setOnClickListener {
//onBoardingViewModel.saveState(true)
findNavController().navigate(R.id.action_introFragment_to_mainActivity)
}
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
OnBoardingFragment.kt
class OnBoardingFragment : Fragment() {
private var _binding: FragmentOnBoardingBinding? = null
private val binding get() = _binding
private var pos = 0
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentOnBoardingBinding.inflate(inflater, container, false)
return binding?.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val fragmentList = arrayListOf(
FirstScreenFragment(),
SecondScreenFragment(),
ThirdScreenFragment()
)
val adapter = ViewPagerAdapter(
fragmentList,
requireActivity().supportFragmentManager,
lifecycle
)
binding?.apply {
onBoardingViewPager.adapter = adapter
onBoardingViewPager.getChildAt(0).overScrollMode = RecyclerView.OVER_SCROLL_NEVER
layoutIndicator.dotsIndicator.attachTo(onBoardingViewPager)
onBoardingViewPager.registerOnPageChangeCallback(object : OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
pos = position
if (pos == fragmentList.size - 1) {
layoutIndicator.btnNext.setOnClickListener {
findNavController().navigate(R.id.action_onBoardingFragment_to_introFragment)
}
}
}
})
layoutIndicator.btnNext.setOnClickListener {
if (pos < fragmentList.size) {
pos++
onBoardingViewPager.currentItem = pos
}
}
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
I've done it by adding a script
binding?.apply {
btnLetsStarted.setOnClickListener {
//onBoardingViewModel.saveState(true)
findNavController().navigate(R.id.action_introFragment_to_mainActivity)
activity?.finish()
}
}
in IntroFragment.kt, is there any other solution?