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

Youtube like BottomNavigation

Youtube like BottomNavigation

kobito-kaba

July 01, 2022
Tweet

More Decks by kobito-kaba

Other Decks in Technology

Transcript

  1. No!

  2. QUIZ: If you click the back key, which screen is

    displayed? 1. Welcome 2. About Click
  3. QUIZ: If you click the back key, which screen is

    displayed? 1. Welcome 2. About Click ?
  4. QUIZ: If you click the back key, which screen is

    displayed? 1. Welcome 2. About Click ✕
  5. Backstack   SavedState   menu A  menu B  ← When the back

    key is pressed ← Can’t decide  which should be restored
  6. C1

  7. C1 C2 C3 D1 D2 Click menu D Click menu

    C Then, the back key will navigate… ① ② ③ ④ ⑤ ⑥ C3 C3 C2 D2 D1 C3 C2 C1 C1 D2 D1 ⑥ ⑤ ④ ③ ② ① ② ③ ① ⑤ ④
  8. class NavStack @Inject constructor(){ data class NavRecord(val screenId: Int, val

    menuId: Int) private val selectedMenu = MutableStateFlow(R.id.none) fun push(screenId: Int, menuId: Int) { ... } fun pop(): NavRecord? { ... } fun peekAt(index: Int) : NavRecord? { ... } val size: Int get() { … } fun findByMenuId(menuId: Int): List<NavRecord> { ... } fun moveToTop(menuId: Int) { ... } fun removeAll(menuId: Int) { ... } }
  9. Navigate Switch menu Go Back if (currentMenuId == nextMenuId) {

    stack.push(screenId, currentMenuId) fragmentManager.replace(currentMenuId, nextScreen.clazz, arguments) } else { stack.moveToTop(nextMenuId) stack.push(screenId, nextMenuId) fragmentManager.saveBackStack(currentMenuId.toString()) fragmentManager.restoreBackStack(nextMenuId.toString()) fragmentManager.replace(nextMenuId, nextScreen.clazz, arguments) }
  10. Navigate case 1: within the same menu if (currentMenuId ==

    nextMenuId) { stack.push(screenId, currentMenuId) fragmentManager.replace(currentMenuId, nextScreen.clazz, arguments) } else { stack.moveToTop(nextMenuId) stack.push(screenId, nextMenuId) fragmentManager.saveBackStack(currentMenuId.toString()) fragmentManager.restoreBackStack(nextMenuId.toString()) fragmentManager.replace(nextMenuId, nextScreen.clazz, arguments) }
  11. if (currentMenuId == nextMenuId) { stack.push(screenId, currentMenuId) fragmentManager.replace(currentMenuId, nextScreen.clazz, arguments)

    } else { stack.moveToTop(nextMenuId) stack.push(screenId, nextMenuId) fragmentManager.saveBackStack(currentMenuId.toString()) fragmentManager.restoreBackStack(nextMenuId.toString()) fragmentManager.replace(nextMenuId, nextScreen.clazz, arguments) } Navigate case 2: across menus
  12. Navigate case 1: within the same menu if (currentMenuId ==

    nextMenuId) { stack.push(screenId, currentMenuId) fragmentManager.replace(currentMenuId, nextScreen.clazz, arguments) } else { stack.moveToTop(nextMenuId) stack.push(screenId, nextMenuId) fragmentManager.saveBackStack(currentMenuId.toString()) fragmentManager.restoreBackStack(nextMenuId.toString()) fragmentManager.replace(nextMenuId, nextScreen.clazz, arguments) }
  13. Navigate case 1: within the same menu if (currentMenuId ==

    nextMenuId) { stack.push(screenId, currentMenuId) fragmentManager.replace(currentMenuId, nextScreen.clazz, arguments) } else { stack.moveToTop(nextMenuId) stack.push(screenId, nextMenuId) fragmentManager.saveBackStack(currentMenuId.toString()) fragmentManager.restoreBackStack(nextMenuId.toString()) fragmentManager.replace(nextMenuId, nextScreen.clazz, arguments) }
  14. Navigate case 1: within the same menu if (currentMenuId ==

    nextMenuId) { stack.push(screenId, currentMenuId) fragmentManager.replace(currentMenuId, nextScreen.clazz, arguments) } else { stack.moveToTop(nextMenuId) stack.push(screenId, nextMenuId) fragmentManager.saveBackStack(currentMenuId.toString()) fragmentManager.restoreBackStack(nextMenuId.toString()) fragmentManager.replace(nextMenuId, nextScreen.clazz, arguments) } fragmentManager.commit { setReorderingAllowed(true) replace(requireNotNull(containerId), clazz.newInstance().apply { arguments = args }) addToBackStack(menuId.toString()) }
  15. Navigate case 1: within the same menu if (currentMenuId ==

    nextMenuId) { stack.push(screenId, currentMenuId) fragmentManager.replace(currentMenuId, nextScreen.clazz, arguments) } else { stack.moveToTop(nextMenuId) stack.push(screenId, nextMenuId) fragmentManager.saveBackStack(currentMenuId.toString()) fragmentManager.restoreBackStack(nextMenuId.toString()) fragmentManager.replace(nextMenuId, nextScreen.clazz, arguments) } fragmentManager.commit { setReorderingAllowed(true) replace(requireNotNull(containerId), clazz.newInstance().apply { arguments = args }) addToBackStack(menuId.toString()) }
  16. if (currentMenuId == nextMenuId) { stack.push(screenId, currentMenuId) fragmentManager.replace(currentMenuId, nextScreen.clazz, arguments)

    } else { stack.moveToTop(nextMenuId) stack.push(screenId, nextMenuId) fragmentManager.saveBackStack(currentMenuId.toString()) fragmentManager.restoreBackStack(nextMenuId.toString()) fragmentManager.replace(nextMenuId, nextScreen.clazz, arguments) } Navigate case 2: across menus
  17. if (currentMenuId == nextMenuId) { stack.push(screenId, currentMenuId) fragmentManager.replace(currentMenuId, nextScreen.clazz, arguments)

    } else { stack.moveToTop(nextMenuId) stack.push(screenId, nextMenuId) fragmentManager.saveBackStack(currentMenuId.toString()) fragmentManager.restoreBackStack(nextMenuId.toString()) fragmentManager.replace(nextMenuId, nextScreen.clazz, arguments) } Navigate case 2: across menus
  18. if (currentMenuId == nextMenuId) { stack.push(screenId, currentMenuId) fragmentManager.replace(currentMenuId, nextScreen.clazz, arguments)

    } else { stack.moveToTop(nextMenuId) stack.push(screenId, nextMenuId) fragmentManager.saveBackStack(currentMenuId.toString()) fragmentManager.restoreBackStack(nextMenuId.toString()) fragmentManager.replace(nextMenuId, nextScreen.clazz, arguments) } Navigate case 2: across menus
  19. if (currentMenuId == nextMenuId) { stack.push(screenId, currentMenuId) fragmentManager.replace(currentMenuId, nextScreen.clazz, arguments)

    } else { stack.moveToTop(nextMenuId) stack.push(screenId, nextMenuId) fragmentManager.saveBackStack(currentMenuId.toString()) fragmentManager.restoreBackStack(nextMenuId.toString()) fragmentManager.replace(nextMenuId, nextScreen.clazz, arguments) } Navigate case 2: across menus
  20. Navigate Switch menu Go Back when { currentMenu.menuId == nextMenu.menuId

    -> { stack.removeAll(currentMenu.menuId) stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.clearBackStack(nextMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } stack.findByMenuId(nextMenu.menuId).isEmpty() -> { stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } else -> { stack.moveToTop(nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.restoreBackStack(nextMenu.menuId.toString()) } }
  21. Switch Menu case 1: re-select menu when { currentMenu.menuId ==

    nextMenu.menuId -> { stack.removeAll(currentMenu.menuId) stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.clearBackStack(nextMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } stack.findByMenuId(nextMenu.menuId).isEmpty() -> { stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } else -> { stack.moveToTop(nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.restoreBackStack(nextMenu.menuId.toString()) } }
  22. Switch Menu case 2: has no records when { currentMenu.menuId

    == nextMenu.menuId -> { stack.removeAll(currentMenu.menuId) stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.clearBackStack(nextMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } stack.findByMenuId(nextMenu.menuId).isEmpty() -> { stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } else -> { stack.moveToTop(nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.restoreBackStack(nextMenu.menuId.toString()) } }
  23. Switch Menu case 3: click the same menu when {

    currentMenu.menuId == nextMenu.menuId -> { stack.removeAll(currentMenu.menuId) stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.clearBackStack(nextMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } stack.findByMenuId(nextMenu.menuId).isEmpty() -> { stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } else -> { stack.moveToTop(nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.restoreBackStack(nextMenu.menuId.toString()) } }
  24. Switch Menu case 1: re-select menu when { currentMenu.menuId ==

    nextMenu.menuId -> { stack.removeAll(currentMenu.menuId) stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.clearBackStack(nextMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } stack.findByMenuId(nextMenu.menuId).isEmpty() -> { stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } else -> { stack.moveToTop(nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.restoreBackStack(nextMenu.menuId.toString()) } }
  25. Switch Menu case 1: re-select menu when { currentMenu.menuId ==

    nextMenu.menuId -> { stack.removeAll(currentMenu.menuId) stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.clearBackStack(nextMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } stack.findByMenuId(nextMenu.menuId).isEmpty() -> { stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } else -> { stack.moveToTop(nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.restoreBackStack(nextMenu.menuId.toString()) } }
  26. Switch Menu case 1: re-select menu when { currentMenu.menuId ==

    nextMenu.menuId -> { stack.removeAll(currentMenu.menuId) stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.clearBackStack(nextMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } stack.findByMenuId(nextMenu.menuId).isEmpty() -> { stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } else -> { stack.moveToTop(nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.restoreBackStack(nextMenu.menuId.toString()) } }
  27. Switch Menu case 2: has no records when { currentMenu.menuId

    == nextMenu.menuId -> { stack.removeAll(currentMenu.menuId) stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.clearBackStack(nextMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } stack.findByMenuId(nextMenu.menuId).isEmpty() -> { stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } else -> { stack.moveToTop(nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.restoreBackStack(nextMenu.menuId.toString()) } }
  28. Switch Menu case 2: has no records when { currentMenu.menuId

    == nextMenu.menuId -> { stack.removeAll(currentMenu.menuId) stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.clearBackStack(nextMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } stack.findByMenuId(nextMenu.menuId).isEmpty() -> { stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } else -> { stack.moveToTop(nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.restoreBackStack(nextMenu.menuId.toString()) } }
  29. Switch Menu case 2: has no records when { currentMenu.menuId

    == nextMenu.menuId -> { stack.removeAll(currentMenu.menuId) stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.clearBackStack(nextMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } stack.findByMenuId(nextMenu.menuId).isEmpty() -> { stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } else -> { stack.moveToTop(nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.restoreBackStack(nextMenu.menuId.toString()) } }
  30. Switch Menu case 3: click the same menu when {

    currentMenu.menuId == nextMenu.menuId -> { stack.removeAll(currentMenu.menuId) stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.clearBackStack(nextMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } stack.findByMenuId(nextMenu.menuId).isEmpty() -> { stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } else -> { stack.moveToTop(nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.restoreBackStack(nextMenu.menuId.toString()) } }
  31. Switch Menu case 3: click the same menu when {

    currentMenu.menuId == nextMenu.menuId -> { stack.removeAll(currentMenu.menuId) stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.clearBackStack(nextMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } stack.findByMenuId(nextMenu.menuId).isEmpty() -> { stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } else -> { stack.moveToTop(nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.restoreBackStack(nextMenu.menuId.toString()) } }
  32. Switch Menu case 3: click the same menu when {

    currentMenu.menuId == nextMenu.menuId -> { stack.removeAll(currentMenu.menuId) stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.clearBackStack(nextMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } stack.findByMenuId(nextMenu.menuId).isEmpty() -> { stack.push(startScreen.screenId, nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.replace(nextMenu.menuId, startScreen.clazz) } else -> { stack.moveToTop(nextMenu.menuId) fragmentManager.saveBackStack(currentMenu.menuId.toString()) fragmentManager.restoreBackStack(nextMenu.menuId.toString()) } }
  33. Navigate Switch menu Go Back when { destRecord == null

    && currentMenuId == graph.startMenuId -> { activity.finish() } destRecord == null -> { stack.push(startScreen.screenId, graph.startMenuId) fragmentManager.replace(graph.startMenuId, startScreen.clazz) } destRecord.menuId == currentMenuId -> { stack.pop() fragmentManager.popBackStack() } else -> { stack.pop() fragmentManager.popBackStack() fragmentManager.restoreBackStack(destRecord.menuId.toString()) } }
  34. Go Back case 1: can’t back at default menu when

    { destRecord == null && currentMenuId == graph.startMenuId -> { activity.finish() } destRecord == null -> { stack.push(startScreen.screenId, graph.startMenuId) fragmentManager.replace(graph.startMenuId, startScreen.clazz) } destRecord.menuId == currentMenuId -> { stack.pop() fragmentManager.popBackStack() } else -> { stack.pop() fragmentManager.popBackStack() fragmentManager.restoreBackStack(destRecord.menuId.toString()) } }
  35. Go Back case 2: can’t back at not default menu

    when { destRecord == null && currentMenuId == graph.startMenuId -> { activity.finish() } destRecord == null -> { stack.push(startScreen.screenId, graph.startMenuId) fragmentManager.replace(graph.startMenuId, startScreen.clazz) } destRecord.menuId == currentMenuId -> { stack.pop() fragmentManager.popBackStack() } else -> { stack.pop() fragmentManager.popBackStack() fragmentManager.restoreBackStack(destRecord.menuId.toString()) } }
  36. Go Back case 3: navigate back inside the same menu

    when { destRecord == null && currentMenuId == graph.startMenuId -> { activity.finish() } destRecord == null -> { stack.push(startScreen.screenId, graph.startMenuId) fragmentManager.replace(graph.startMenuId, startScreen.clazz) } destRecord.menuId == currentMenuId -> { stack.pop() fragmentManager.popBackStack() } else -> { stack.pop() fragmentManager.popBackStack() fragmentManager.restoreBackStack(destRecord.menuId.toString()) } }
  37. Go Back case 4: navigate back across menus when {

    destRecord == null && currentMenuId == graph.startMenuId -> { activity.finish() } destRecord == null -> { stack.push(startScreen.screenId, graph.startMenuId) fragmentManager.replace(graph.startMenuId, startScreen.clazz) } destRecord.menuId == currentMenuId -> { stack.pop() fragmentManager.popBackStack() } else -> { stack.pop() fragmentManager.popBackStack() fragmentManager.restoreBackStack(destRecord.menuId.toString()) } }
  38. in Summary: - Fragment supports Multiple Backstack - BottomNavigationView works

    weirdly - You can make it by yourself - I don’t want to make it
  39. in Summary: - Fragment supports Multiple Backstack - BottomNavigationView works

    weirdly - You can make it by yourself - I don’t want to make it
  40. in Summary: - Fragment supports Multiple Backstack - BottomNavigationView works

    weirdly - You can make it by yourself - I don’t want to make it
  41. in Summary: - Fragment supports Multiple Backstack - BottomNavigationView works

    weirdly - You can make it by yourself - I don’t want to make it
  42. in Summary: - Fragment supports Multiple Backstack - BottomNavigationView works

    weirdly - You can make it by yourself - I don’t want to make it