Why the viewmodel class is not called when the second time its navigated to same fragment?

Viewed 40

I have a picker fragmet which is navigated from many other fragments. So the behaviour is to close the fragment and return the selected item to the parent fragment. So the code written to get this behaviour is the following. On Recyclerview Item click

PickerFragment

 onItemClick = { binding ->
                    binding.stack?.let {
                        val bundle = bundleOf("stack" to it)
                        if (args.actionArg == R.id.stackEditFragment) {
                                 //put bundle
                                )
                            )
                        }
                        if(args.actionArg == R.id.transferBetweenStacksFragment){
                           //bundle
                        }


                        val navOptions =
                            NavOptions.Builder().setPopUpTo(args.actionArg, false).build()
                        findNavController().navigate(
                            args.actionArg,
                            bundle,
                            navOptions
                        )
                    }

Now I have to open the picker fragment when 'from' and 'to' buttons of parent fragment is clicked. And I have to pass the title (from/to) as args(stackArg) and I am setting that as the title of the fragment in the picker viewmodel class.

PickerViewModel

val toolbar = ToolbarViewModel(
        args.stackArg.toUnresolved(),
        leftButton = ToolbarButton.Back {
            navigate(NavigationCommand.Up)
        }
    )

Below is the fragment code from which the PickerFragment is called.

ParentFragment

fun selectStackClicked(view: View) {
        
       val title = if (view.id == R.id.ll_select_from) {
            "Pay from"
        } else {
            "Pay to "
        }
        Log.d("----",title)
        navigate(
            NavigationCommand.To( 
                TransferBetweenStacksFragmentDirections
                    .actionTransferBetweenStacksFragmentToStackPickerFragment(R.id.transferBetweenStacksFragment, title)
            )
        )
    }

Here I am setting the title (stackArg) as 'from' or 'to' based on from which view I have clicked it . So on the first click (either 'from' or 'to'),it works fine ,the fragment is opened and viewmodel is also called and title is set correctly. But when I click (from/to) next time ,the PickerFragment is opened but the PickerViewmodel is not getting called and so the title remains the previous title only .

Can someone help with this ? Why is the viewmodel class not getting called second time ?

0 Answers
Related