Home > database >  IllegalArgumentException: Destination id == 0 can only be used in conjunction with a valid navOption
IllegalArgumentException: Destination id == 0 can only be used in conjunction with a valid navOption

Time:12-15

In one Fragment, I am using NavController from a context menu, which has a nested menu.

If I try to navigate to one of the nested menu items it gives me the following error:

java.lang.IllegalArgumentException: Destination id == 0 can only be used in conjunction with a valid navOptions.popUpTo

How can I avoid this error and navigate to the nested destinations?

Menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group
        android:checkableBehavior="single"
        android:title="file"
        >

        <item
            android:id="@ id/nav_misa"
            android:title="@string/lbl_misa_hoy" />
        <item
            android:id="@ id/nav_homilias"
            android:title="@string/lbl_homilias_hoy" />
        <item
            android:id="@ id/nav_lecturas"
            android:title="Lecturas" />
        <item
            android:id="@ id/nav_comentarios"
            android:title="@string/lbl_comentarios_hoy" />

    </group>
    <group android:checkableBehavior="single">
        <item
            android:id="@ id/nav_santo"
            android:title="@string/lbl_santo_hoy" />
    </group>

    <item android:title="Liturgia de las Horas">
        <menu>
            <item
                android:id="@ id/nav_mixto"
                android:title="Oficio Laudes" />
            <item
                android:id="@ id/nav_oficio"
                android:title="Oficio" />
            <item
                android:id="@ id/nav_laudes"
                android:title="Laudes" />
            <item
                android:id="@ id/nav_tercia"
                android:title="Tercia" />
            <item
                android:id="@ id/nav_sexta"
                android:title="Sexta" />
            <item
                android:id="@ id/nav_nona"
                android:title="Nona" />
            <item
                android:id="@ id/nav_visperas"
                android:title="Vísperas" />
            <item
                android:id="@ id/nav_completas"
                android:title="Completas" />

        </menu>
    </item>
</menu>

Code for navigation

public boolean onContextItemSelected(MenuItem item) {
    Bundle bundle = new Bundle();
    bundle.putString("FECHA", mDate);
    NavController navController = NavHostFragment.findNavController(this);
    navController.navigate(item.getItemId(),bundle);
    return true;
}

CodePudding user response:

You have to check whether the menuId is 0 or not, this is because your nested menu's root ("Liturgia de las Horas") has no ID and thus it will return 0

public boolean onContextItemSelected(MenuItem item) {
    if (item.getItemId() == 0) {
        return true;
    }
    Bundle bundle = new Bundle();
    bundle.putString("FECHA", mDate);
    NavController navController = NavHostFragment.findNavController(this);
    navController.navigate(item.getItemId(),bundle);
    return true;
}
  • Related