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

Oh, the places you'll go! Cracking Navigation on Android

Oh, the places you'll go! Cracking Navigation on Android

" You’re building your app, you’re out on your way,
Activity to fragment, you’ll add deep links one day.
Notifications, transitions, you pass arguments, too,
Just a messy piece of cake, for a developer like you!
But then the user hits back, or up or away -
What should happen? You’re lost! The road starts to sway!

They talk about launch modes, affinity, activity stack...
We will deep-dive to those,
then learn the new stuff from Jetpack! "

*** Presented at Android Summit, and Andorid NYC meetup, on August 2018.

Britt Barak

August 16, 2018
Tweet

More Decks by Britt Barak

Other Decks in Programming

Transcript

  1. Cracking Navigation on Android @BrittBarak

  2. @BrittBarak Congratulation Today is your day. You’re building your app!

    You’re off and away @BrittBarak
  3. @BrittBarak You’re on your own You know what you know

    And you are the DEV that decides where your USER will go! @BrittBarak
  4. Britt Barak DevRel @ Nexmo Google Developer Expert Women Techmakers

    Israel Britt Barak @brittBarak
  5. @BrittBarak App Activity @BrittBarak

  6. @BrittBarak App Activities are welcome! com.nexmo.brittApp Activity Activity2 Activity3 @BrittBarak

  7. @BrittBarak App Activities are welcome! com.nexmo.brittApp Activity Activity2 Activity3 @BrittBarak

  8. @BrittBarak com.vonage.britt App @BrittBarak

  9. @BrittBarak Our lives are PERFECT! @BrittBarak

  10. @BrittBarak Expect when they’re not. Because sometime, they won’t @BrittBarak

  11. @BrittBarak

  12. @BrittBarak

  13. @BrittBarak

  14. @BrittBarak

  15. @BrittBarak

  16. @BrittBarak Modify Navigation Behaviour • AndroidManifest: launchMode • Code: Intent

    Flags @BrittBarak
  17. @BrittBarak Standard

  18. @BrittBarak conversationList

  19. @BrittBarak conversationList conversation

  20. @BrittBarak conversationList conversation conversation

  21. @BrittBarak conversationList conversation conversation conversation

  22. @BrittBarak Single Top

  23. @BrittBarak conversation conversationList

  24. @BrittBarak conversation conversation conversationList

  25. @BrittBarak conversation onNewIntent() conversationList

  26. @BrittBarak conversation conversation conversationList

  27. @BrittBarak conversation conversation phoneCall conversationList

  28. @BrittBarak conversation conversation phoneCall conversationList

  29. @BrittBarak conversation conversation phoneCall conversation conversationList

  30. @BrittBarak conversation conversation conversationList conversation phoneCall

  31. @BrittBarak convoList

  32. @BrittBarak convoList Search

  33. @BrittBarak convoList Search onNewIntent()

  34. @BrittBarak convoList Search onNewIntent()

  35. @BrittBarak convoList Search

  36. @BrittBarak convoList

  37. @BrittBarak But I want 1 instance!

  38. @BrittBarak

  39. @BrittBarak Single Task

  40. @BrittBarak conversationList

  41. @BrittBarak conversationList conversation

  42. @BrittBarak conversation conversationList phoneCall singleTask

  43. @BrittBarak conversationList conversation phoneCall singleTask @BrittBarak

  44. @BrittBarak conversationList conversation @BrittBarak phoneCall singleTask

  45. @BrittBarak conversationList conversation @BrittBarak phoneCall singleTask

  46. @BrittBarak conversationList conversation conversation @BrittBarak phoneCall singleTask

  47. @BrittBarak conversationList conversation conversation @BrittBarak phoneCall singleTask

  48. @BrittBarak conversationList conversation phoneCall singleTask conversation @BrittBarak phoneCall singleTask

  49. @BrittBarak conversationList conversation phoneCall singleTask @BrittBarak phoneCall singleTask

  50. @BrittBarak conversationList conversation onNewIntent( ) @BrittBarak phoneCall singleTask

  51. @BrittBarak conversationList conversation phoneCall singleTask @BrittBarak

  52. @BrittBarak conversationList conversation phoneCall singleTask @BrittBarak

  53. @BrittBarak conversationList conversation @BrittBarak

  54. @BrittBarak conversationList conversation

  55. @BrittBarak conversationList

  56. @BrittBarak conversation phoneCall singleTask

  57. @BrittBarak Single Instance

  58. @BrittBarak conversationList

  59. @BrittBarak conversationList conversation

  60. @BrittBarak conversationList phoneCall singleInstance conversation

  61. @BrittBarak conversationList phoneCall singleInstance conversation @BrittBarak

  62. @BrittBarak conversationList phoneCall singleInstance conversation @BrittBarak

  63. @BrittBarak conversationList phoneCall singleInstance conversation @BrittBarak

  64. @BrittBarak conversationList conversation phoneCall singleInstance conversation @BrittBarak Nah ah!

  65. @BrittBarak conversationList conversation phoneCall singleInstance conversation @BrittBarak

  66. @BrittBarak conversationList conversation phoneCall singleInstance conversation @BrittBarak

  67. @BrittBarak conversationList conversation phoneCall singleInstance conversation phoneCall singleInstance @BrittBarak

  68. @BrittBarak conversationList conversation phoneCall singleInstance phoneCall singleInstance conversation @BrittBarak

  69. @BrittBarak conversationList conversation phoneCall singleInstance onNewIntent( ) conversation @BrittBarak

  70. @BrittBarak conversationList phoneCall singleInstance conversation conversation @BrittBarak

  71. @BrittBarak Make your task visible!! @BrittBarak

  72. @BrittBarak Document

  73. @BrittBarak

  74. @BrittBarak

  75. @BrittBarak We’re done! we have only Activities! @BrittBarak

  76. @BrittBarak Expect when we don’t. Because sometime, we won’t @BrittBarak

  77. @BrittBarak App Activity Activities are welcome! com.nexmo.brittApp @BrittBarak

  78. @BrittBarak App Activity Activities are welcome! com.nexmo.brittApp Fragment1 @BrittBarak

  79. @BrittBarak App Activity Activities are welcome! com.nexmo.brittApp Fragment1 Fragment2 @BrittBarak

  80. @BrittBarak App Activity Activities are welcome! com.nexmo.brittApp Fragment1 Fragment2 @BrittBarak

  81. @BrittBarak App Activity Activities are welcome! com.nexmo.brittApp Fragment1 @BrittBarak

  82. @BrittBarak App Activities are welcome! com.nexmo.brittApp @BrittBarak

  83. @BrittBarak @BrittBarak

  84. @BrittBarak NavigationComponent • Alpha • Android Studio 3.2+ • Focus

    on Fragments on a single Activity
  85. @BrittBarak <navigation android:id="@+id/nav_graph"> //. . . </navigation>

  86. @BrittBarak @BrittBarak

  87. @BrittBarak 1. Choose Destinations

  88. @BrittBarak @BrittBarak

  89. @BrittBarak convoList conversation phoneCall Search newConvo Video Text

  90. @BrittBarak

  91. @BrittBarak

  92. @BrittBarak

  93. @BrittBarak

  94. @BrittBarak convoList

  95. @BrittBarak 2. Build Flow

  96. @BrittBarak

  97. @BrittBarak convoList conversation

  98. @BrittBarak convoList conversation phoneCall Search newConvo Video Text

  99. @BrittBarak convoList conversation phoneCall Search newConvo Video Text

  100. @BrittBarak 3. Host the Graph

  101. @BrittBarak Host the Graph NavHost layout_activity.xml

  102. @BrittBarak Host the Graph NavHostFragment NavHost

  103. @BrittBarak layout_activity.xml

  104. @BrittBarak layout_activity.xml <fragment …/>

  105. @BrittBarak layout_activity.xml <fragment android:id=“@+id/nav_host_fragment" …/>

  106. @BrittBarak layout_activity.xml <fragment android:id=“@+id/nav_host_fragment" android:name= “androidx.navigation.fragment. NavHostFragment" …/>

  107. @BrittBarak layout_activity.xml <fragment android:id=“@+id/nav_host_fragment" android:name= “androidx.navigation.fragment. NavHostFragment" app:navGraph="@navigation/nav_graph" …/>

  108. @BrittBarak layout_activity.xml <fragment android:id=“@+id/nav_host_fragment" android:name= “androidx.navigation.fragment. NavHostFragment" app:navGraph="@navigation/nav_graph" app:defaultNavHost=“true” …/>

  109. @BrittBarak 4. Attach NavHost to UP override fun onSupportNavigateUp()= findNavController(R.id.nav_host_fragment)

    .navigateUp()
  110. @BrittBarak 5. Navigate 1. Find NavHost •NavHostFragment.findNavController(Fragment) •Navigation.findNavController(Activity, int viewId)

    •Navigation.findNavController(View)

  111. @BrittBarak 5. Navigate 2. Execute navigation navController. navigate(R.id.viewTransactionsAction) 


  112. @BrittBarak 5.1 Navigate from Menu <item android:id=“@+id/convo_list_frag” android:icon=“@drawable/ic_list” android:title=“@string/all_convos”/>

  113. @BrittBarak 5.1 Navigate from Menu override fun onOptionsItemSelected(item: MenuItem): Boolean

    { }
  114. @BrittBarak 5.1 Navigate from Menu override fun onOptionsItemSelected(item: MenuItem): Boolean

    { return NavigationUI. onNavDestinationSelected(item,navController) }
  115. @BrittBarak 5.1 Navigate from Menu override fun onOptionsItemSelected(item: MenuItem): Boolean

    { return NavigationUI. onNavDestinationSelected(item,navController) || super.onOptionsItemSelected(item) }
  116. @BrittBarak

  117. @BrittBarak convoList conversation ConvId

  118. @BrittBarak SafeArgs

  119. @BrittBarak

  120. @BrittBarak convoList conversation ConvId convoListDirection conversationArgs

  121. @BrittBarak conversationListFragment val action = ConvoListDirections.

  122. @BrittBarak conversationListFragment val action = ConvoListDirections. openConvAction(myConversationId)

  123. @BrittBarak conversationListFragment val action = ConvoListDirections. openConvAction(myConversationId) navController.navigate(action)

  124. @BrittBarak ConversationFragment val id = ConversationFragmentArgs.

  125. @BrittBarak ConversationFragment val id = ConversationFragmentArgs. fromBundle(arguments).convId

  126. @BrittBarak

  127. @BrittBarak Deeplinks • Url link • PendingIntent • from a

    notification / widget / slice
  128. @BrittBarak

  129. @BrittBarak AndroidManifest.xml <activity android:name=".MainActivity"> <!— . . . . —>

    <nav-graph android:value=“@navigation/nav_graph”/> </activity>
  130. @BrittBarak PendingIntent for Deeplinks PendingIntent deeplink = navController.createDeepLink() .setDestination(R.id.conversationFragment) .setArguments(args)

    .createPendingIntent();
  131. @BrittBarak PendingIntent for Deeplinks PendingIntent deeplink = navController.createDeepLink() .setDestination(R.id.conversationFragment) .setArguments(args)

    .createPendingIntent();
  132. @BrittBarak PendingIntent for Deeplinks PendingIntent deeplink = navController.createDeepLink() .setDestination(R.id.conversationFragment) .setArguments(args)

    .createPendingIntent();
  133. @BrittBarak convoList conversation Handles Back Stack

  134. @BrittBarak What else can we do with Actions

  135. @BrittBarak What else can we do with Actions

  136. @BrittBarak What else can we do with Actions

  137. @BrittBarak ChatFragment CallFragment

  138. @BrittBarak ChatFragment CallFragment Enter Exit

  139. @BrittBarak ChatFragment CallFragment

  140. @BrittBarak ChatFragment CallFragment

  141. @BrittBarak ChatFragment PopEnter PopExit CallFragment

  142. @BrittBarak @BrittBarak

  143. @BrittBarak “And will you succeed? Yes! You will, indeed! (98

    an 3/4 percent guaranteed)” DEV, you’ll move fragments! @BrittBarak
  144. @BrittBarak Thank you! Keep in touch @BrittBarak