Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Arch Components - Navigation

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →

Arch Components - Navigation

Avatar for Luis Mierez

Luis Mierez

August 29, 2018

Other Decks in Technology

Transcript

  1. NAVIGATION ABOUT ME ▸ Android Developer for about 5 year

    ▸ Works at Homesnap ▸ Just moved back from DC ▸ Work remote now
  2. NAVIGATION PRINCIPLES OF NAVIGATION ▸ fixed starting destination ▸ navigation

    state is a stack ▸ up button never exits app ▸ up and back should be the same ▸ deep linking or navigation = same stack
  3. NAVIGATION ADD DEPENDENCIES dependencies { def nav_version = "1.0.0-alpha05" implementation

    "android.arch.navigation:navigation-fragment:$nav_version" implementation "android.arch.navigation:navigation-ui:$nav_version" // Add -ktx for kotlin implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version" implementation "android.arch.navigation:navigation-ui-ktx:$nav_version" }
  4. NAVIGATION ADD NAVIGATION RESOURCE ▸ Right-click on navigation resource directory

    ▸ New > Android Resource File ▸ Input name <?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" android:id="@+id/main_navigation"> </navigation>
  5. NAVIGATION NAVIGATION LAYOUT <?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"/> </com.google.android.material.appbar.AppBarLayout> <fragment app:layout_behavior="@string/appbar_scrolling_view_behavior" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" app:navGraph="@navigation/main_navigation" app:defaultNavHost="true"/> </androidx.coordinatorlayout.widget.CoordinatorLayout> Handles the navigation
  6. NAVIGATION HOOK UP NAVIGATION class MainActivity : AppCompatActivity() { override

    fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.navigation_acitivty_layout) setSupportActionBar(toolbar) // From -ktx setupActionBarWithNavController(findNavController(R.id.nav_host_fragment)) // Otherwise need to call // NavigationUI.setupActionBarWithNavController(this, // Navigation.findNavController(this, R.id.nav_host_fragment)) } override fun onSupportNavigateUp(): Boolean { return findNavController(R.id.nav_host_fragment).navigateUp() } }
  7. NAVIGATION ▸ Pass data between fragment using ViewModel (or Room)

    ▸ Single activity for single flow (not necessarily for the whole app) ▸ Don’t have to use fragments (custom navigators) ▸ ActivityNavigator can help with migrating ▸ Use safeArgs (if possible)
  8. NAVIGATION EXAMPLE APP (EXERCISE LOGGER) ▸ Let’s you create a

    new exercise entry ▸ View log of exercises ▸ Update user info ▸ Update exercise from log
  9. NAVIGATION COMPONENTS USED ▸ Kotlin ▸ Room ▸ LiveData ▸

    Coroutines ▸ and of course Navigation