Agung
Agung

Reputation: 13803

how to back to previous destination programatically using navigation controller in Android?

I have login process like the image below:

enter image description here

the user will be navigated from login screen (in the middle) to change password screen (in the right) using the code below:

 val changePassword = LoginUsingEmailFragmentDirections.actionToChangePasswordFragment()
Navigation.findNavController(fragmentView).navigate(changePassword)

I want if the user successfully change the password, I want to make the user back to login screen programatically.

If I use the old way navigation using a lot of activities, then I usually using finish()

I little bit confused what is the right way to achieve this. do I need to make action first in the graph and then using the similar code like above ? or is there any more convenient or better way to do this ?

Upvotes: 6

Views: 1884

Answers (1)

Hussnain Haidar
Hussnain Haidar

Reputation: 2258

You can navigate up to login screen on button click listener like below:

btn.setOnClickListener{
 Navigation.findNavController(fragmentView).navigateUp()
}

Upvotes: 11

Related Questions