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

Practical Implementation of Up Navigation

Practical Implementation of Up Navigation

potatotips #38

Keisuke Kobayashi

March 22, 2017
Tweet

More Decks by Keisuke Kobayashi

Other Decks in Programming

Transcript

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

    Qiita: @kobakei • Twitter: @ksk_kbys • Android, Rails, React
  2. Back vs Up • Back • Navigate to previous screen

    which may be other app • Up • Navigate to parent screen in same app
  3. EC app • Timeline > Detail • Timeline > Search

    > Detail • Timeline > Detail > Detail • Recommendation, similar items, … • Deep link > Detail • Notification, Google search result, …
  4. Calling Activity#finish() ? @Override public boolean onOptionsItemSelected(MenuItem item) { switch

    (item.getItemId()) { case android.R.id.home: finish(); return true; } return super.onOptionsItemSelected(item); }
  5. 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…
  6. 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); }
  7. What is wrong? 1. Recreated parent activity • Scroll position,

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

    bottom navigation etc 2. Always same parent activity regardless of where it comes from
  9. 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); }
  10. What is wrong? 1. Recreated parent activity • Scroll position,

    bottom navigation etc 2. Always same parent activity regardless of where it comes from
  11. 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; }
  12. 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); } } }
  13. 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); }
  14. 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