Skip to content

Latest commit

ย 

History

History
91 lines (66 loc) ยท 3.81 KB

0723-android.md

File metadata and controls

91 lines (66 loc) ยท 3.81 KB

2020.07.23

๐Ÿ“š๊ณต๋ถ€ํ•œ ๊ฑฐ List๐Ÿ“š

  • ์•ˆ๋“œ๋กœ์ด๋“œ - bottom navigation with fragment ์ฝ”๋“œ ๋ถ„์„

Bottom Navigation Activity

android studio์—์„œ New > Activity > Bottom Navigation Activity๋ฅผ ํด๋ฆญํ•˜๋ฉด ์ž๋™์œผ๋กœ ํŒŒ์ผ๋“ค๊ณผ ํด๋”๋“ค์ด ์ƒ์„ฑ๋œ๋‹ค.

1. ์ดˆ๊ธฐ:์‹คํ–‰ํ™”๋ฉด

Home Dashboard Notifications
image image image

2. ์ดˆ๊ธฐ:๋””๋ ‰ํ„ฐ๋ฆฌ ๊ตฌ์กฐ

app/main/java/com.example.[APPNAME]

|_MainActivity.kt
|_/ui
   |/dashboard
     |__DashBoardFragment.kt
     |__DashBoardViewModel.kt
   |/home
     |__HomeFragment.kt
     |__HomeViewModel.kt
   |_/notifications
     |__NotificationsFragment.kt
     |__NotificationsViewModel.kt

app/main/res

|_/drawable
   |__ic_dashboard_black_24dp.xml
   |__ic_home_black_24dp.xml
   |__ic_launcher_background.xml
   |__ic_notifications_black_24dp.xml
|_/drawable-v24*
|_/layout
   |__activity_main.xml
   |__fragment_dashboard.xml
   |__fragment_home.xml
   |__fragment_notifications.xml
|_/menu
   |__bottom_nav_menu.xml
|_/navigation
   |__mobile_navigation.xml
|_/values
   |__colors.xml
   |__dimens.xml
   |__strings.xml
   |__styles.xml

Fragment

Android Developer:Fragment ์„ค๋ช… ๋ฐ”๋กœ๊ฐ€๊ธฐ

  • ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ํ•˜๋‚˜์˜ ์•กํ‹ฐ๋น„ํ‹ฐ์— ๊ฒฐํ•ฉํ•˜์—ฌ ์ฐฝ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ UI๋ฅผ ๋นŒ๋“œํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ•˜๋‚˜์˜ ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ์—ฌ๋Ÿฌ ์•กํ‹ฐ๋น„ํ‹ฐ์—์„œ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ”„๋ž˜๊ทธ๋จผํŠธ๋Š” ํ•ญ์ƒ ์•กํ‹ฐ๋น„ํ‹ฐ ๋‚ด์—์„œ ํ˜ธ์ŠคํŒ…๋˜์–ด์•ผ ํ•˜๋ฉฐ ํ•ด๋‹น ํ”„๋ž˜๊ทธ๋จผํŠธ์˜ ์ˆ˜๋ช… ์ฃผ๊ธฐ๋Š” ํ˜ธ์ŠคํŠธ ์•กํ‹ฐ๋น„ํ‹ฐ์˜ ์ˆ˜๋ช… ์ฃผ๊ธฐ์— ์ง์ ‘์ ์œผ๋กœ ์˜ํ–ฅ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค.

LifeCycle

onCreate() : ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ˜ธ์ถœ๋œ๋‹ค. ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ์ผ์‹œ์ •์ง€๋˜๊ฑฐ๋‚˜ ์ค‘๋‹จ๋˜์—ˆ๋‹ค๊ฐ€ ์žฌ๊ฐœ๋˜์—ˆ์„ ๋•Œ ์œ ์ง€ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ณต๊ฐ„์ž„.

onCreateView() : ์‹œ์Šคํ…œ์€ ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ์ž์‹ ์˜ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ฒ˜์Œ์œผ๋กœ ๊ทธ๋ฆด ์‹œ๊ฐ„์ด ๋˜๋ฉด ์ด๊ฒƒ์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ํ”„๋ž˜๊ทธ๋จผํŠธ์— ๋งž๋Š” UI๋ฅผ ๊ทธ๋ฆฌ๋ ค๋ฉด ๋ฉ”์„œ๋“œ์—์„œ View๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ํ”„๋ž˜๊ทธ๋จผํŠธ ๋ ˆ์ด์•„์›ƒ์˜ ๋ฃจํŠธ์ž…๋‹ˆ๋‹ค. ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ UI๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ null์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

onPause() : ์‹œ์Šคํ…œ์ด ์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์€ ์‚ฌ์šฉ์ž๊ฐ€ ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ๋– ๋‚œ๋‹ค๋Š” ๊ฒƒ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ฒซ ๋ฒˆ์งธ ์‹ ํ˜ธ์ž…๋‹ˆ๋‹ค(๋‹ค๋งŒ ํ•ญ์ƒ ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ์†Œ๋ฉธ ์ค‘์ด๋ผ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค). ์ผ๋ฐ˜์ ์œผ๋กœ ์—ฌ๊ธฐ์—์„œ ํ˜„์žฌ ์‚ฌ์šฉ์ž ์„ธ์…˜์„ ๋„˜์–ด์„œ ์ง€์†๋˜์–ด์•ผ ํ•˜๋Š” ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ปค๋ฐ‹ํ•ฉ๋‹ˆ๋‹ค(์‚ฌ์šฉ์ž๊ฐ€ ๋Œ์•„์˜ค์ง€ ์•Š์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค).

ViewModel

Android Developers:ViewModel ์„ค๋ช… ๋ฐ”๋กœ๊ฐ€๊ธฐ

Medium_ViewModels : A Simple Example ๊ธ€ ๋ฐ”๋กœ๊ฐ€๊ธฐ

  • ViewModel์€ UI์™€ ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ(=Activity๋‚˜ Fragment์˜ ๋ฐ์ดํ„ฐ)๋ฅผ ์ƒ๋ช…์ฃผ๊ธฐ๋™์•ˆ ์œ ์ง€ํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๊ณ ์•ˆ๋œ ๊ฒƒ์ด๋‹ค.
  • ViewModel์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
    1. ViewMode์„ ์ƒ์†ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ UI ์ปจํŠธ๋กค๋Ÿฌ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌ์‹œํ‚จ๋‹ค.
    2. ViewModel๊ณผ UI controller์˜ ๋ฐ์ดํ„ฐ ์†Œํ†ต์„ ๊ตฌํ˜„ํ•œ๋‹ค.
    3. UI controller์—์„œ ViewModel์„ ์‚ฌ์šฉํ•œ๋‹ค.