androidkotlinandroid-fragmentsnavigationandroid-ktx

backstack with navigation component


using the navigation component on two fragments, my app currently destroys a fragment when navigateUp() is called (using the setupActionBarWithNavController()), and therefore when I enter the deleted fragment, all progress is lost, I am trying to change that my adding the fragment to a backstack (and only one instance of the fragment) but I've been struggling with that...

Here's some code:

MainActivity:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        loginRepository = LoginRepository()
        if (!loginRepository.checkLoggedInState()) {
            goToLoginActivity()
        }

        Log.d("FirebaseMain", "onCreate: ${loginRepository.checkLoggedInState()}")

        //MyApplication.app.currentUser
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

        navController = Navigation.findNavController(this, R.id.nav_host_fragment)

        // setup custom action bar
        setSupportActionBar(findViewById(R.id.toolbar))

        // adds a return button in the ActionBar.
        NavigationUI.setupActionBarWithNavController(this, navController)
    }

FormFragment:

(the fragment I want to save its progress and add to backstack)

class FormFragment : Fragment() {

    private lateinit var binding: FragmentFormBinding

    private val checkListRecyclerAdapter = CheckListRecyclerAdapter(
        hashMapOf(
            0 to "mirrors",
            1 to "blinkers1",
            2 to "blinkers11",
            3 to "blin4kers",
            4 to "blink3e1123rs",
            5 to "blink6ers",
            6 to "blin53kers",
            7 to "blin8kers",
            8 to "blin7kers",
            9 to "blin43kers",
            10 to "blin32kers",
            11 to "blin322kers",

            )
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_form, container, false)

        binding.rvCheckList.apply {
            // this is used to load the data sequentially from top to bottom,
            this.layoutManager = LinearLayoutManager(context)

            // the adapter that loads the data into the list
            this.adapter = checkListRecyclerAdapter
        }

        return binding.root
    }

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

    }

}

Thanks in advance for any help!


Solution

  • I've been struggling with some similar to this. Fragments in Navigation Component doesnt keep their state. If you keep your data progress while navigate, you need create a SharedViewModel scoped to a Navigation Graph.

    In Fragment:

    private val navGraphScopedViewModel by navGraphViewModels<NavGraphViewModel>(R.id.your_nav_graph)
    

    Create this class:

    class NavGraphViewModel : ViewModel() {
        var sharedData=  CustomObject()
    }
    

    And from all your fragments you can access and set data:

     val data = navGraphScopedViewModel.sharedData
    

    I hope I've helped you