iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
😺

Receiving parameters from a destination screen using Navigation

に公開

I have updated and revised an article I previously posted on Note.

While watching this video, I learned that starting from Navigation 2.3, it is possible to return a value when navigating back to a screen. I have transcribed the login sample from the video.

Fragment accessible only after logging in

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val navController = findNavController()
    val stateHandle = navController.currentBackStackEntry!!.savedStateHandle
    stateHandle.getLiveData<Boolean>(LoginFragment.LOGIN_SUCCESSFUL)
        .observe(viewLifecycleOwner, Observer { success ->
            if (success) {
                loadData()
            } else {
                val startDistination = navController.graph.startDestination
                navController.navigate(startDistination, null, navOptions {
                    popUpTo(startDistination) {
                        inclusive = true
                    }
                })
            }
    })

    if (!(activity as MainActivity).getIsLogin()) {
        navController.navigate(R.id.action_mainFragment_to_loginFragment)
    }
}

This is a Fragment that can only be accessed after logging in.

    if (!(activity as MainActivity).getIsLogin()) {
        navController.navigate(R.id.action_mainFragment_to_loginFragment)
    }

This part determines whether the user is already logged in.
In a real-world scenario, login verification would typically be performed using ViewModel -> Repository.

stateHandle.getLiveData<Boolean>(LoginFragment.LOGIN_SUCCESSFUL)

This is the part that receives the return value from the destination login screen.
Since it can be received as LiveData, you just need to observe it.

LoginFragment

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val navController = findNavController()
    val savedStateHandle = navController.previousBackStackEntry!!.savedStateHandle

    view.findViewById<Button>(R.id.button).setOnClickListener {
        savedStateHandle.set(LOGIN_SUCCESSFUL, true)
        navController.popBackStack()
    }
    view.findViewById<Button>(R.id.button3).setOnClickListener {
        savedStateHandle.set(LOGIN_SUCCESSFUL, false)
        navController.popBackStack()
    }
}

This is the Fragment that handles the login process.
In this case, I have provided a login success button and a login failure button, each setting a result before returning to the Fragment that can only be accessed after logging in.

savedStateHandle.set(LOGIN_SUCCESSFUL, true)
navController.popBackStack()

Here, the key and value to be passed to the previous screen are set before navigating back.

Sample Code

https://github.com/sobaya-0141/AllFlow/tree/login

Discussion