choieuihyun / Upbit-Clone

๋น„ํŠธ์ฝ”์ธ ํ˜„๋ฌผ ETF ์Šน์ธ๊ธฐ๋… ์—…๋น„ํŠธ ํด๋ก ์ฝ”๋”ฉ
0 stars 0 forks source link

JetPackNavigation + BottomNavigation #13

Open choieuihyun opened 9 months ago

choieuihyun commented 9 months ago

๐Ÿš€ ๊ฐœ๋ฐœ ์ด์Šˆ

bottomNavigation์˜ ์•„์ดํ…œ์„ ๋ˆŒ๋Ÿฌ ํ™”๋ฉด์„ ์ด๋™ํ•˜๋ ค๊ณ  ํ•˜๋ฉด java.lang.NullPointerException ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ

๐Ÿค” ํ•ด๊ฒฐ ๋ฐฉ์•ˆ ๊ณ ์•ˆ

๋ฌธ์ œ์˜ ์›์ธ์ด ๋ช‡ ๊ฐ€์ง€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

  1. NavController ์ดˆ๊ธฐํ™” ๋ฌธ์ œ

  2. ๋ฆฌ์†Œ์Šค ID ๋ถˆ์ผ์น˜ : BottomNavigation์˜ ๋ฉ”๋‰ด item Id์™€ NavController์˜ Navigation Graph์— ์ •์˜๋œ ๋ชฉ์ ์ง€ ๊ฐ„์— ID ์ผ์น˜

๋ญ ์ด ์™ธ์—๋„ ์žˆ๋Š”๋ฐ ๋‚ด๊ฐ€ ์—ฌ๋Ÿฌ๋ฒˆ ๊นŒ๋จน๊ธฐ๋„ ํ–ˆ๊ณ  ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค๋„ ๋งŽ์ด ์‹ค์ˆ˜ํ•˜๋Š” 2๋ฒˆ. ์ด๊ฒŒ ๊ฑฐ์˜ ๋ฌธ์ œ๊ฐ™๋‹ค.

choieuihyun commented 9 months ago

์ „์— ์‚ฌ์šฉํ•ด๋ดค๋˜ ํ”„๋กœ์ ํŠธ์—์„œ ์ž˜ ๋๊ณ , ๊ทธ๊ฑธ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜์˜€๋Š”๋ฐ ๋ฌด์—‡์ด ๋ฌธ์ œ์ผ๊นŒ

choieuihyun commented 9 months ago

์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ณณ์—์„œ ์ •์˜๋œ ๋ถ€๋ถ„์˜ ๊ณ„์ธต์„ ํƒ€๊ณ  ๋“ค์–ด๊ฐ€์„œ ๋ณธ ๊ฒฐ๊ณผ

onNavDestinationSelected ๋ฉ”์„œ๋“œ์˜

if (navController.currentDestination!!.parent!!.findNode(item.itemId)) ์ด ๋ถ€๋ถ„๊ณผ

setupWithNavController ๋ฉ”์„œ๋“œ์˜

navigationBarView.setOnItemSelectedListener { item -> onNavDestinationSelected( item, navController ) } ์ด ๋ถ€๋ถ„์ด ๋ฌธ์ œ๋‹ค. ์ด ์™ธ์—๋„ ๋งŽ์€ ์—๋Ÿฌ ์Šคํƒ์ด ๋œจ์ง€๋งŒ ์ด ๋‘ ๊ฐœ๊ฐ€ ์—ฐ๊ฒฐ๋˜๋Š” ๋ถ€๋ถ„์ด๊ธฐ์— ๊ฐ€์žฅ ์œ ๋ ฅํ•ด ๋ณด์ž„.

  1. ๋‚ด๊ฐ€ bottomNavigation์˜ ์•„์ดํ…œ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ํ•ด๋‹น item ์ •๋ณด์™€ navController๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ onNavDestinationSelected๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค.
  2. onNavDestinationSelected์—์„œ ์ „๋‹ฌ๋ฐ›์€ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ navController์˜ ํ˜„์žฌ ๋„์ฐฉ์ง€์ , ๋ถ€๋ชจ์ง€์ (์—ฐ๊ฒฐ๋˜๋Š” ๋‘ ์ง€์  ์ถ”์ •)์˜ ๋…ธ๋“œ๋ฅผ ๋‚ด๊ฐ€ ํด๋ฆญํ•œ item์˜ id๋ฅผ ์ด์šฉํ•ด ์ฐพ๋Š”๋‹ค.

์—๋Ÿฌ์ฝ”๋“œ ์˜๋ฏธ ํ•ด์„์€ ์ด ์ •๋„๊ฐ€ ๋˜๊ฒ ์ง€๋งŒ ์‚ฌ์‹ค ์ด๊ฒŒ ์ค‘์š”ํ•œ๊ฒŒ ์•„๋‹ˆ๋‹ค. NPE๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์œผ๋กœ ๋ณด์•„ '์ด๊ฒŒ ์–ด๋””์—์„œ ๋ฐœ์ƒํ–ˆ๋ƒ'๊ฐ€ ์ค‘์š”ํ•œ ํฌ์ธํŠธ์ธ๋ฐ findNode() ๊ฐ€ ์ •์˜๋œ ๋ถ€๋ถ„์„ ๋ณด์•˜์„ ๋•Œ null์— ๋Œ€ํ•ด์„œ ์—˜๋น„์Šค ์—ฐ์‚ฐ์ž๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์–ด์žˆ์—ˆ์Œ. ์ด์™€ ๊ฐ™์€๊ฒฝ์šฐ๋Š” ์ด๋Ÿฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜์„ ๊ฒƒ์ž„. ๊ทธ๋Ÿฌ๋ฉด ๋‚จ์€๊ฒŒ currentDestination!!.parent!! ์—ฌ๊ธฐ ์ด null์ด ์•„๋‹ˆ๋ผ๊ณ  ํ˜ธ์–ธ์žฅ๋‹ด ํ•˜๊ณ ์žˆ๋Š” ์ด ๋ถ€๋ถ„์ด๋‹ค. ์—ฌ๊ธฐ๊ฐ€ null์ผ ์ˆ˜๊ฐ€ ์—†๋Š”๋ฐ ๋‚ด๊ฐ€ ๊ตฌํ˜„ํ•œ ๊ณณ์—์„œ๋Š” null์ด๋ผ NPE๋œจ๋ฉด์„œ ์ข…๋ฃŒ๊ฐ€ ๋œ๋‹ค๊ณ  ์ƒ๊ฐ๋œ๋‹ค. ๊ทผ๋ฐ ๊ทธ๋ž˜๋„ ์™œ ํ•ด๊ฒฐ์ด ์•ˆ๋˜์ง€ ํ•  ์ˆ˜ ์žˆ๋Š”๊ฑฐ ๋‹ค ํ•ด๋ดค๋Š”๋ฐ? ์ด๋ ‡๊ฒŒ ํ‰์†Œ์— ์ž˜๋งŒ ์‚ฌ์šฉํ•˜๋˜๊ฒŒ ์•ˆ๋˜๊ณ  ์ด์œ  ์ฐพ๊ธฐ๋„ ํž˜๋“ ๊ฒŒ ์ œ์ผ ํ™”๋‚œ๋‹ค.

choieuihyun commented 9 months ago

ํ•ด๊ฒฐํ–ˆ๋‹ค.

์˜ˆ์ „์— ํ–ˆ๋˜๊ฑฐ ๊ทธ๋Œ€๋กœ ๊ฐ–๋‹ค์“ฐ๋‹ˆ๊นŒ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๊ฑฐ์˜€๋‹ค.

navController.popBackStack() ์—ฌ๊ธฐ๊ฐ€ ๋ฌธ์ œ์ธ๊ฑธ ๊ทธ๋ƒฅ ์‹ ๊ฒฝ๋„ ๋ชป์ผ๋‹ค.

ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ๋ฐฑ์Šคํƒ์— ์Œ“์ธ๋‹ค๋Š” ๊ฐœ๋…์€ ์•Œ๊ณ ์žˆ์—ˆ๋Š”๋ฐ ์•ฑ์˜ ๊ตฌ์กฐ์— ๋”ฐ๋ผ์„œ๋Š” ์ ์šฉ๋  ์ˆ˜๋„ ์žˆ์—ˆ์ง€๋งŒ

์ด๋ฒˆ ์•ฑ์˜ ๊ตฌ์กฐ๋Š” ์• ์ดˆ์— ์‹œ์ž‘์ ์˜ ์ฐจ์ด๋กœ ์ธํ•ด ํ™”๋ฉด์ด ์ „ํ™˜๋  ๋•Œ ํ˜„์žฌ ๋ฐฑ์Šคํƒ์— ํ•˜๋‚˜๋ฐ–์— ์—†๋Š”๋ฐ popbackstack()์œผ๋กœ ํ™”๋ฉด ์ „ํ™˜ํ•˜๋ฉด์„œ

์—†์• ๋ฒ„๋ฆด๋ผ๊ณ  ํ•˜๋‹ˆ๊นŒ stack ์ž์ฒด๊ฐ€ ๋น„์–ด๋ฒ„๋ ค null์ด ๋˜์–ด ๊ทธ๋Ÿฐ ๊ฒƒ ๊ฐ™๋‹ค.(popbackstack() ์ •์˜ ํ™•์ธํ•ด๋ด„)