This is example of how android navigation can be implemented in android using java and xml.
Android navigation helps user to navigate through different pages of app and come back to previous pages.
⚠ This example is uses java + xml not kotlin.
Android navigation provides several benefits like :-
- Full support of fragment transaction.
- Transition between the switching of fragment
- Read more about benefits at Benefits and features
implementation "androidx.navigation:navigation-fragment:$nav_version"implementation "androidx.navigation:navigation-ui:$nav_version"
- It is a xml resource created under
res/navigation/any_file_name.xml. - Android studio provides navigation editor to edit navigation resource.
- This resource defines NavDestination or Navigation Destinations i.e. - from which fragment to which fragment transaction should allowed etc.
- Basically it connects all the fragments to each other so that user can jump from one fragment to another.
- It is also called navHostFragment and is created using
<fragment></fragment>element. - It should be present in the activity in which we need to perform navigation because during navigation the fragments are replaced in
<fragment></fragment>(navHostFragment). - Generally it should be present in
activity_main.xmlassuming that we are making app by following single activity architecture. - Two most required attributes should be present in
<fragment></fragment>element.android:name="androidx.navigation.fragment.NavHostFragment"app:navGraph="@navigation/your_nav_graph"
- A NavController object is associated with each NavHostFragment or
<fragment></fragment>element if they have both attributes (discussed in 2nd point) defined properly. - NavController helps in switching between fragments according to the pattern declared in NavGraph.
- NavController associated with NavHostFragment can be accessed in java file (MainActivity.java) using
Navigation.findNavController() - NavController object provides methods like
navigate(),popBackStack()to navigate to a fragment or go back to previous fragment respectively.
- Create different fragments for each page. (ex - HomeFragment, ProfileFragment, AboutFragment)
- Make sure to put each fragments in different package within application's package.
As in this example - fragments HomeFragment, AboutFragment and ProfileFragment are present in about_page, home_page and profile_page packages respectively within the application's package (com.example.android_navigation).
- Create a resource directory in
res/with namenavigation. - Create a navigation resource file inside
res/navigation. - Open navigation resource file and add new destination by clicking
new destinationoption in navigation editor of android studio. - Add all the fragments and connect them accordingly as you want your user to navigate through.
As for this example I want user to navigate fromHomeFragment to AboutFragmentand fromHomeFragment to ProfileFragmentso I connected home fragment with profile and about fragments in navigation editor.
Notice that as you are connecting the fragments in navigation editor, in component tree (bottom right side) there are action id's are adding something likeaction_homeFragment_to_profileFragment. Remember it, these id's are gonna be used while navigation form java code. - Make sure to select a startDestination fragment else you app might crash,
- Go into activity_main.xml and create a FrameLayout inside which create
<fragment></fragment>element. - Carefully set an id like -
android:id="@+id/nav_host_frag"to<fragment></fragment>element. - Carefully set
android:name="androidx.navigation.fragment.NavHostFragment"attribute as it is. - Carefully attach your navigation resource file (created in previous section) in
app:navGraph="@navigation/your_nav_res_file"attribute. - Create 3 buttons home, about, profile in activity_main.xml file inside main constrain layout.
Note that its only if you are following this example, these button will help us to notice navigation by interaction, you can implement same logic in different ways.
- Go into MainActivity.java and get access of NavController in previously created NavHostFragment using
Navigation.findNavController(findViewById(R.id.nav_host_frag));
Note thatR.id.nav_host_fragshould be id of<fragment></fragment>element created in previous step. - It will return NavController type object store it as shown in this example's MainActivity.java
- Use
navigate()method to navigate to the desired action that we noticed while creating NavGraph. (check MainActivity.java for reference) - Use
popBackStack()method to go back to last navigated fragment.
This example is created with reference to the android docs and my experiments.
To learn more about navigation check out android docs :-