如何处理NavController目标更改?

时间:2019-11-04 03:52:17

标签: android android-jetpack android-jetpack-navigation

我想实现addOnDestinationChangedListener,但是没有运气。我已经尝试过自己实现,但ID不匹配

$end = \Carbon\Carbon::parse($request->tgl_mulai)->addDays(3);

我尝试记录它,这是结果

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

1 个答案:

答案 0 :(得分:0)

您正在使用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;
    }
});