Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between global actions and destination specific action in android navigation graphs?

In android navigation graphs, we have global actions and action within a destination.

Using global actions, multiple destinations can reuse an action. So what would be the advantage of using actions within destinations?
In other words, what is the drawback of using global actions in the whole navigation graph?

like image 303
Abhimanyu Avatar asked Jan 27 '23 02:01

Abhimanyu


1 Answers

It is helpful to think of your graph as a class hierarchy: adding an action is like adding a method to the base class. That makes the action available to all subclasses (i.e., all destinations within that graph).

A key part of object oriented programming languages is encapsulation:

Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties' direct access to them.

When you add an action to an individual action, besides making it obvious in the Navigation Editor, you provide a level of encapsulation, ensuring that action is only used from that specific destination.

The difference is much more apparent when using Safe Args as Safe Args generates exactly the matching class hierarchy of Directions classes that match your graph: global actions appear on YourNavGraphIdDirections base classes and destination specific actions appear on the individual YourFragmentDirections subclasses.

Therefore by abusing global actions, you're cluttering the list of available methods (i.e., actions) available on each destination, making it harder for you to determine what actually is an appropriate action for a given destination.

like image 61
ianhanniballake Avatar answered Feb 04 '23 03:02

ianhanniballake