App Navigation in Jetpack Library Android

App Navigation


We can navigate between activities and between fragments in an activity.

Activity 
Activity operates as a frame that contains the UI fragments and can provide UI elements that surround the fragment. 

Fragment 
Android introduced fragment in API level 11 primarily to support more dynamic and flexible UI design on large screens such as tablets. UI fragment generally operate like a view within the activity's layout. But like an activity we must create a subclass of fragment to use it. This gives you a layout along with a convenient place to put UI logic, the foundation of a reusable UI component. 
With fragment we can pretty much treat as the operating system's entry point to the app. Since, most of our UI ends up being implemented in the fragments, but OS can only open Activity. 

Common Differences 
  • Within activity we tell Android which layout to use by calling setContentView in onCreate, the activity inflates the layout and places it correctly within the activity's layout hierarchy. 
  • With fragments, we manually inflate and return the inflated layout within the onCreateView method which is independent of onCreate
  • Since Activities inherit from the context class but fragments do not, you'll need to use the context property within a fragment to access to AppData typically associated with the context.  Eg. string and image resources 
          context!!.getString(R.string.app_name)
          context!!.getDrawable(R.drawable.ic_launcher)



Navigation 
You can navigate between different activities and between fragments in an activity.
As we navigate through activities in Android the previous activities from within the app as well as the previous apps are arranged in a stack that we call the BackStack. This BackStack is ordered upon the order in which each activity is opened so an application that has a title screen as first screen followed by the app main screen activity will have the current activity as the top of the stack. Pressing the back button will pop the previous used activity.
Fragments can have the similar back stack. The entire stack is contained within the activity. All of this is controlled by a class called FragmentManager. When the fragment is instantiated by the FragmentManager the fragment transaction can be optionally be added to the fragment Back Stack.


How to design your Navigation ? 
You can design your navigation with following models :
  • either 1 activity containing a series of fragments
  • series of activity 
  • combination of both techniques 


Principles of Navigation

  1. There's always a starting place 
  2. You can always go back (LIFO stack principle) i.e. it has the backstack 
  3. Up goes Back (mostly), go back to the previous destination 
  4. If the user is at start destination the up button (Any button on left side of toolbar )should not be shown 


Steps: 

Add navigation library, Gradle dependencies 
ext{
// navigation version
navigationVersion = '2.3.0-alpha04'

}
// navigation component
implementation "androidx.navigation:navigation-fragment-ktx:$navigationVersion"
implementation "androidx.navigation:navigation-ui-ktx:$navigationVersion"



Navigating Flow 
  1. Adding the Navigation Graph Resource to your project 
  2. Adding the NavHostFragment 
  3. Adding Fragments to the Navigation Graph 
  4. Connecting Fragments in the Graph with Actions 
  5. Create onClickListener 
  6. Find Navigation Controller 
  7. Navigate with our Action 

Adding the Navigation Host Fragment to your MainActivity 
<fragment
android:id="@+id/navigationHost"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="@navigation/home_navigation"
app:defaultNavHost="true"
/>

we set defaultNavHost = true , which means that this navigation host will intercept the system back key. We have to set this explicitly because we can have multiple simultaneous navigation hosts and we want only one of the navigation to handle the back button. 

How to actually navigate ? 
We need the instance of the navigation controller, which is a class that we use to manage navigation within our Navigation Host Fragment. 
The Navigation Host Fragment is the parent in the view hierarchy of our current fragment i.e. the HomeFragment, this means that we can traverse up this hierarchy to find the NavHostFragment from any view in our fragment, including the info button that we use to click and navigate to another destination fragment. 
Navigation provides us a helper function called findNavController that takes the view, finds the enclosing NavHostFragment and returns the navigation controller for that NavHostFragment. 

binding.floatingActionButton.setOnClickListener { view ->
// (activity as MainActivity).pushInfoFragment()
// Navigation.findNavController(view).navigate(R.id.action_homeFragment_to_infoFragment)
view.findNavController().navigate(R.id.action_homeFragment_to_infoFragment)
}





















Comments

Popular Posts