J'ai la navigation principale: SplashFragment -> RegistrationFragment -> RootFragment
<fragment
Android:id="@+id/splashFragment"
Android:name="com.low6.low6.features.splash.SplashFragment"
Android:label="Splash"
tools:layout="@layout/fragment_splash" >
<action
Android:id="@+id/action_next"
app:clearTask="true"
app:destination="@id/registrationFragment" />
</fragment>
<fragment
Android:id="@+id/registrationFragment"
Android:name="com.low6.low6.features.registration.RegistrationFragment"
Android:label="Register">
<action
Android:id="@+id/action_next"
app:clearTask="true"
app:destination="@id/rootFragment" />
</fragment>
<fragment
Android:id="@+id/rootFragment"
Android:name="com.low6.low6.core.RootFragment"
Android:label="@string/home"
tools:layout="@layout/fragment_root" />
Et j'ai imbriqué la navigation d'enregistrement: RegistrationPersonalFragment -> RegistrationContactFragment -> RegistrationSecurityFragment
<fragment
Android:id="@+id/registrationPersonalFragment"
Android:name="com.low6.low6.features.registration.RegistrationPersonalFragment"
Android:label="Register">
<action
Android:id="@+id/action_next"
app:destination="@+id/registrationContactFragment" />
</fragment>
<fragment
Android:id="@+id/registrationContactFragment"
Android:name="com.low6.low6.features.registration.RegistrationContactFragment"
Android:label="Register">
<action
Android:id="@+id/action_next"
app:destination="@+id/registrationSecurityFragment" />
</fragment>
<fragment
Android:id="@+id/registrationSecurityFragment"
Android:name="com.low6.low6.features.registration.RegistrationSecurityFragment"
Android:label="Register">
<action
Android:id="@+id/action_next"
app:destination="@+id/rootFragment" />
</fragment>
Comment rediriger du dernier RegistrationSecurityFragment imbriqué vers RootFragment en utilisant le composant de navigation Jetpack?
Actuellement
<action
Android:id="@+id/action_next"
app:destination="@+id/rootFragment" />
Et
navigateTo(R.id.action_next)
Donne moi
Java.lang.IllegalArgumentException: navigation destination com.xxx:id/rootFragment referenced from action com.xxx:id/action_next is unknown to this NavController
at androidx.navigation.NavController.navigate(NavController.Java:691)
at androidx.navigation.NavController.navigate(NavController.Java:648)
at androidx.navigation.NavController.navigate(NavController.Java:634)
at com.xxx.core.BaseFragment.navigateTo(BaseFragment.kt:73)
at com.xxx.core.BaseFragment.navigateTo$default(BaseFragment.kt:66)
at com.xxx.features.registration.RegistrationSecurityFragment$epoxyController$1$$special$$inlined$button$lambda$1.onClick(RegistrationSecurityFragment.kt:106)
at Android.view.View.performClick(View.Java:6597)
at Android.view.View.performClickInternal(View.Java:6574)
at Android.view.View.access$3100(View.Java:778)
at Android.view.View$PerformClick.run(View.Java:25885)
at Android.os.Handler.handleCallback(Handler.Java:873)
at Android.os.Handler.dispatchMessage(Handler.Java:99)
at Android.os.Looper.loop(Looper.Java:193)
at Android.app.ActivityThread.main(ActivityThread.Java:6669)
at Java.lang.reflect.Method.invoke(Native Method)
at com.Android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.Java:493)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:858)
Grâce à la réponse de @Vladimir, j'ai trouvé cette solution
val mainNavView = requireActivity().findViewById<View>(R.id.mainNavFragment)
Navigation.findNavController(mainNavView).navigate(R.id.action_next)
Vous pouvez le faire comme ça;
in Child Fragment
// parentFragment = getParentFragment() for Java
(parentFragment as MyParentFragment).myNavigationHandler(myArguments)
dans le fragment parent
fun myNavigationHandler(myArguments) {
Navigation.findNavController(binding.root)
.navigate(MyFragmentDirections.actionMyAction(myArguments))
}
Dans votre code, vous pouvez transmettre l'ID de ressource de l'action globale à la méthode navigate()
pour chaque élément d'interface utilisateur.
your_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(view).navigate(R.id.main_fragment);
}
});