0

I am using Navigation Component to navigate through pages in my app.

I have a ProfileFragment and users might navigate to this fragment from different fragments(like HomeFragment, ArticleFragment, and ...).

In my ProfileFragment how should I know where to navigate the user to the previous page before they opened Profile. When the user pressed Back Button how am I supposed to detect the right action.

In my fragment's toolbar, I have an arrow (ImageButton) for navigation to the previous page too. How should I handle it in its onClickListener?

Here you can see the graph of my navigation.xml

2
  • When you will press the back button the app will load the previous fragment automatically. You will not have to do anything. Commented Aug 22, 2020 at 17:55
  • Check out my update question please Commented Aug 22, 2020 at 18:29

1 Answer 1

1

When user clicks the back arrow in the Toolbar, you can call findNavController().popBackStack(). This will move user to the last fragment in the back stack, regardless of which one was visible before (before moving to ProfileFragment).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.