gpt4 book ai didi

android - 如何处理 NavController 目的地变化?

转载 作者:行者123 更新时间:2023-12-04 23:56:37 24 4
gpt4 key购买 nike

我想实现 addOnDestinationChangedListener,但是没有成功。我已经尝试自己实现它,但是 ID 不匹配

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;
}
});

我尝试记录它,这是结果

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

最佳答案

您正在使用 R.id.action_numberEntryFragment_to_otpFragment - 即,您正在使用的操作的 ID。然而,OnDestinationChangedListener接收您最终实际要去的目的地的 ID - 即 app:destination <action> 上的字段.

因此您应该使用目标 ID(只需猜测您的目标 ID 是什么):

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;
}
});

关于android - 如何处理 NavController 目的地变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58686987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com