Difference between Navigating one fragment to another using fragment id and action id using nav graph in android jetpack library

huangapple go评论58阅读模式
英文:

Difference between Navigating one fragment to another using fragment id and action id using nav graph in android jetpack library

问题

我在我的安卓项目中使用Jetpack Navigation图。通常,要导航从一个片段到另一个片段,我们在导航图中连接它们,然后像这样调用:

findNavController().navigate(R.id.action_navigation_login_to_navigation_send_code)

但是,我们也可以这样调用片段,而不创建链接:

findNavController().navigate(R.id.navigation_send_code)

基本区别是什么?在我的理解中,两者都完成相同的工作。

英文:

I am using jetpack navigation graph in my android project. Normally to navigate one to another fragment we connect a link between them in nav graph and call like this

 findNavController().navigate(R.id.action_navigation_login_to_navigation_send_code)

But We can call the fragment this way as well without creating the link.

 findNavController().navigate(R.id.navigation_send_code)

What is the basic difference? In my sense, both do the same work.

答案1

得分: 1

Both versions back to the same navController navigate() function that takes in an integer resource id whether it's a fragment id or an action id.

Navigate to a destination from the current navigation graph. This supports both navigating via an {@link NavDestination#getAction(int) action} and directly navigating to a destination.

Internally it calls this navigate() version where it examines whether it's an action id or not.

If it's an action id, then it gets the destination id from it; if not an action id it consider it as a destination id and continue on; notice the comments in below:

@IdRes int destId = resId;
final NavAction navAction = currentNode.getAction(resId);
Bundle combinedArgs = null;
if (navAction != null) { // here the destId is an action id
	if (navOptions == null) {
		navOptions = navAction.getNavOptions();
	}
	destId = navAction.getDestinationId(); // resets the destId to the destination id
	Bundle navActionArgs = navAction.getDefaultArguments();
	if (navActionArgs != null) {
		combinedArgs = new Bundle();
		combinedArgs.putAll(navActionArgs);
	}
}

// Now destId is the destination fragment id.. continue on...

So, technically no difference, but adding an action id adds an extra step of getting the fragment id which is nothing in terms of scalable apps.

The other difference, that if you put an actionId instead of a fragment id, you'd have an extra feature of navOptions that can be returned from the navGraph like adding enter/exit animation.

英文:

Both versions back to the same navController navigate() function that takes in an integer resource id whether it's a fragment id or an action id.

> Navigate to a destination from the current navigation graph. This supports both navigating
via an {@link NavDestination#getAction(int) action} and directly navigating to a destination.

Internally it calls this navigate() version where it examines whether it's an action id or not.

If it's an action id, then it gets the destination id from it; if not an action id it consider it as a destination id and continue on; notice the comments in below:

@IdRes int destId = resId;
final NavAction navAction = currentNode.getAction(resId);
Bundle combinedArgs = null;
if (navAction != null) { // here the destId is an action id
	if (navOptions == null) {
		navOptions = navAction.getNavOptions();
	}
	destId = navAction.getDestinationId(); // resets the destId to the destination id
	Bundle navActionArgs = navAction.getDefaultArguments();
	if (navActionArgs != null) {
		combinedArgs = new Bundle();
		combinedArgs.putAll(navActionArgs);
	}
}

// Now destId is the destination fragment id.. continue on...

So, technically no difference, but adding an action id adds an extra step of getting the fragment id which is nothing in terms of scalable apps.

The other difference, that if you put an actionId instead of a fragment id, you'd have an extra feature of navOptions that can be returned from the navGraph like adding enter/exit animation.

huangapple
  • 本文由 发表于 2023年2月16日 14:18:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定