Reputation: 33
I want to implement the addOnDestinationChangedListener but, no luck. I've tried implementing it on my own but, the ids doesn't match
NavController mController = Navigation.findNavController(this, R.id.nav_auth_fragment);
mController.addOnDestinationChangedListener((controller, destination, arguments) -> {
switch (destination.getId()){
case R.id.action_loginFragment_to_numberEntryFragment:
Toast.makeText(this, "Welcome to number Entry", Toast.LENGTH_SHORT).show();
break;
case R.id.action_numberEntryFragment_to_otpFragment:
Toast.makeText(this, "Enter your OTP", Toast.LENGTH_SHORT).show();
break;
case R.id.action_otpFragment_to_userRegistrationFragment:
Toast.makeText(this, "Your number is verified!", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
});
I tried logging it and here is the results
2019-11-04 11:39:17.179 26830-26830/com.example.myapp D/AuthActivity: 2131230930 == 2131230775 = false
2019-11-04 11:39:17.179 26830-26830/com.example.myapp D/AuthActivity: 2131230930 == 2131230781 = false
2019-11-04 11:39:17.180 26830-26830/com.example.myapp D/AuthActivity: 2131230930 == 2131230782 = false
where 2131230930 is the destination.getId() and (2131230775, 2131230781, 2131230782) is the resource ids
even when I'm at the destination, the id still doesn't match with the resource id
Upvotes: 3
Views: 2018
Reputation: 199805
You're using R.id.action_numberEntryFragment_to_otpFragment
- i.e., the IDs of the actions you're using. However, the OnDestinationChangedListener
receives the ID of the destination you end up actually going to - i.e., the app:destination
field on the <action>
.
Therefore you should be using the destination IDs (just guessing on what your destination IDs are):
mController.addOnDestinationChangedListener((controller, destination, arguments) -> {
switch (destination.getId()){
case R.id.numberEntryFragment:
Toast.makeText(this, "Welcome to number Entry", Toast.LENGTH_SHORT).show();
break;
case R.id.otpFragment:
Toast.makeText(this, "Enter your OTP", Toast.LENGTH_SHORT).show();
break;
case R.id.userRegistrationFragment:
Toast.makeText(this, "Your number is verified!", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
});
Upvotes: 5