I am new in using navigation component I have 2 activity each one have 3 fragments should I create for each activity a navigation host graph or there are a way to use only one nav graph for 2 activities?
2 Answers
For Navigation components with multiple-activities, you shouldn't start activities using Intents, instead, you need to add activities as destinations to the nav_graph
.
Should I create for each activity a navigation host graph or there are a way to use only one nav graph for 2 activities ?
Yes you should, for n activities, you should have n nav graphs, or less (in case you need to make some activities like normal activities that are not involved in the navigation)
So, in your case you have 2 activities, then you should have nav_grap1 & nav_grap2.
And supposing you need to move from activity1, fragment1 to activity2, then what you need to do:
- In
nav_graph1
add<activity2>
as a destination. - Create an action in
nav_graph1
from fragment1 toactivity2
The nav graph of activity1 should look to something like:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/navigation"
app:startDestination="@id/fragment1_1">
<fragment
android:id="@+id/fragment1_1"
android:name="com.example.android.xx.Fragment1_1"
android:label="Fragment1_1" >
<action
android:id="@+id/action_fragment1_1_to_secondActivity"
app:destination="@id/secondActivity" />
</fragment>
<fragment
android:id="@+id/fragment1_2"
android:name="com.example.android.xx.Fragment1_2"
android:label="Fragment1_2" />
<activity
android:id="@+id/secondActivity"
android:name="com.example.android.xx.SecondActivity"
android:label="SecondActivity" />
</navigation>
- Whenever you want to move to activity2, then just use the different
navigate()
methods of the navigationController; for instance:
findNavController().navigate(R.id.action_fragment1_1_to_secondActivity)
For more info check documentation, and check this question

- 37,492
- 7
- 60
- 84
-
1Oh thank you it help me so much. i wanted to navigate from fragment1 to activity 2 but i couldn't figure out how i can do that . So thankk you for your answer – Hager Khaled Aug 12 '21 at 00:58
-
What about If I need to move from activity 1 to activity 2 ? – Mohamed Mohamed Taha Mar 21 '22 at 11:46
-
@MohamedMohamedTaha In activity1, you should have some startDestination fragment associated to the navgraph of activity1; then we would say you want to move from this startDestination (or any destination within that navgraph) to activity2, so, as in the answer you can do that with an action `findNavController().navigate(R.id.action_someFragmentInActivity1_to_Activity2)` ... If you just use activities without fragments, then why you use the navigation components which is mainly based on fragments ? – Zain Mar 21 '22 at 12:38
-
I use Navigation bottom and Navigation view I did for navigation bottom navGraph and navigation view also I did anther navGraph the main activity there bottom navigation but when I open navigationview I want open anther graph without open any fragment from bottom navigation. – Mohamed Mohamed Taha Mar 21 '22 at 17:55
You can use an Intent to move from Activity 1 to Activity 2.
Each Activity will need its own NavGraph to host and navigate within the 3 Fragments. So will you will be using Navigation Component to move from one fragment to another within the hosting Activity.
You may also to decide to just have One Activity if your Architecture allows.

- 4,865
- 3
- 34
- 50