Using 2 navigation approaches simultaneously. Remove last backstack fragment

19 views Asked by At

I have some problem. My project use old approach and navigation component. And I have the following situation: I want to create chat with some users. Once I have selected the users I should go to the chat screen. Chat activity open by old approach (by startActivity). And if I tap on back navigate button I'll open the user selection fragment, although I need to open the chat list fragment. So My path is Chat List -> User selection Fragment -> Chat Activity. But I need after selected user I need the next way: Chat List -> Chat Activity (delete User selection Fragment from navigation component backstack).

I found decision, as using bool flag if we go to another activity and set as flag true, and if we went back, we do popBackStack() twice. It's works, but in this case, the application stores the fragment and viewModel in memory, which is not very good.

Perhaps there is a good solution on how to destroy a fragment before opening another fragment (remove User selection Fragment and open Chat activity by startActivity).

Now it’s difficult to switch completely to navigation component.

Thanks!

1

There are 1 answers

0
Alex On

For now I decided to use the following approach: when I added to Done fun findNavController().popBackStack() line. Now my code:

parent fragment (as super):

override fun onDoneBtnClicked() {
        viewModel.onDoneBtnClicked(this)
    }

child fragment:

override fun onDoneBtnClicked() {
        super.onDoneBtnClicked()
        findNavController().popBackStack()
    }

It's working and the user does not see unnecessary animations.