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

Practical Implementation of Up Navigation

Practical Implementation of Up Navigation

potatotips #38

85cab5fdf09afe3ee78ce3667681915a?s=128

Keisuke Kobayashi

March 22, 2017
Tweet

Transcript

  1. Practical Implementation of Up Navigation Keisuke Kobayashi (@kobakei) potatotips #38

  2. About me • Keisuke Kobayashi • Coubic Inc • GitHub,

    Qiita: @kobakei • Twitter: @ksk_kbys • Android, Rails, React
  3. Up Navigation?

  4. Up Back

  5. Back vs Up • Back • Navigate to previous screen

    which may be other app • Up • Navigate to parent screen in same app
  6. Example

  7. EC app • Timeline > Detail • Timeline > Search

    > Detail • Timeline > Detail > Detail • Recommendation, similar items, … • Deep link > Detail • Notification, Google search result, …
  8. Navigation Flow 5JNFMJOF 4FBSDI %FUBJM (deep link)

  9. Navigation Flow 5JNFMJOF 4FBSDI %FUBJM (deep link) From search

  10. Navigation Flow 5JNFMJOF 4FBSDI %FUBJM (deep link) Otherwise From search

  11. How to implement?

  12. Just calling 
 Activity#finish() ?

  13. Calling Activity#finish() ? @Override public boolean onOptionsItemSelected(MenuItem item) { switch

    (item.getItemId()) { case android.R.id.home: finish(); return true; } return super.onOptionsItemSelected(item); }
  14. Calling Activity#finish() ? • May be OK in simple flow

    • NG in Timeline > Detail > Detail flow • It opens previous Detail screen… • NG in deep link flow • It closes app…
  15. OK, I will copy & paste from developer.android.com

  16. developer.android.com @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) {

    // Respond to the action bar's Up/Home button case android.R.id.home: NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); }
  17. Something wrong…

  18. What is wrong? 1. Recreated parent activity • Scroll position,

    bottom navigation etc 2. Always same parent activity regardless of where it comes from
  19. What is wrong? 1. Recreated parent activity • Scroll position,

    bottom navigation etc 2. Always same parent activity regardless of where it comes from
  20. Avoid recreating parent @Override public boolean onOptionsItemSelected(MenuItem item) { switch

    (item.getItemId()) { case android.R.id.home: Intent upIntent = NavUtils.getParentActivityIntent(this); upIntent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(upIntent); finish(); return true; } return super.onOptionsItemSelected(item); }
  21. What is wrong? 1. Recreated parent activity • Scroll position,

    bottom navigation etc 2. Always same parent activity regardless of where it comes from
  22. Set parent activity class to bundle extra // DetailActivity#createIntent public

    static Intent createIntent( @NonNull Context context, @Nullable Class<? extends Activity> parentClass) { Intent intent = new Intent(context, DetailActivity.class); if (parentClass != null) { intent.putExtra(KEY_PARENT, parentClass.getName()); } return intent; }
  23. Restore parent activity class
 from bundle extra // Parent activity

    class private Class parentClass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // … String parentClassName = getIntent().getStringExtra(KEY_PARENT); if (TextUtils.isEmpty(parentClassName)) { parentClass = MainActivity.class; } else { try { parentClass = Class.forName(parentClassName); } catch (ClassNotFoundException e) { Timber.e(e); } } }
  24. Create up intent with 
 parent activity class @Override public

    boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: Intent upIntent = new Intent(getApplicationContext(), parentClass); upIntent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(upIntent); finish(); return true; } return super.onOptionsItemSelected(item); }
  25. Complete

  26. Conclusion • Explained how to implement UP navigation in complex

    flow • Key points • FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGE_TOP • Set parent class in bundle extra and use for Intent to parent activity
  27. Appendix • ͪΐͬͱෳࡶͳUPϘλϯͷ࣮૷ in Qiita • http://qiita.com/kobakei/items/ ae1a6d2062f34d3f6771 • DroidKaigi

    2017 app
  28. Thanks!