๐Ÿ”ฅTIL๐Ÿ”ฅ์ŠคํŒŒ๋ฅดํƒ€ | intent

hyihyiยท2023๋…„ 11์›” 20์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
1/70
post-thumbnail

BMI ๊ณ„์‚ฐ๊ธฐ

[๋‚ด๋ฐฐ์บ ]Android ๊ฐœ๋ฐœ ์ข…ํ•ฉ๋ฐ˜ 1์ฃผ์ฐจ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  BMI ๊ณ„์‚ฐ๊ธฐ๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.

Intent

Intent๋Š” ๋ฉ”์‹œ์ง• ๊ฐ์ฒด๋กœ, ๋‹ค๋ฅธ ์•ฑ ๊ตฌ์„ฑ ์š”์†Œ๋กœ๋ถ€ํ„ฐ ์ž‘์—…์„ ์š”์ฒญํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

Intent์˜ ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ ์‚ฌ๋ก€

  1. Activity๋Š” ์•ฑ ์•ˆ์˜ ๋‹จ์ผ ํ™”๋ฉด์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. Activity์˜ ์ƒˆ ์ธ์Šคํ„ด์Šค๋ฅผ ์‹œ์ž‘ํ•˜๋ ค๋ฉด Intent๋ฅผ startActivity()๋กœ ์ „๋‹ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. Intent๋Š” ์‹œ์ž‘ํ•  ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์„ค๋ช…ํ•˜๊ณ  ๋ชจ๋“  ํ•„์ˆ˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์Šต๋‹ˆ๋‹ค.
    ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์„ ๋•Œ ๊ฒฐ๊ณผ๋ฅผ ์ˆ˜์‹ ํ•˜๋ ค๋ฉด, startActivityForResult()๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์•กํ‹ฐ๋น„ํ‹ฐ๋Š” ํ•ด๋‹น ๊ฒฐ๊ณผ๋ฅผ ์ด ์•กํ‹ฐ๋น„ํ‹ฐ์˜ onActivityResult() ์ฝœ๋ฐฑ์—์„œ ๋ณ„๋„์˜ Intent ๊ฐ์ฒด๋กœ ์ˆ˜์‹ ํ•ฉ๋‹ˆ๋‹ค.
  1. ์„œ๋น„์Šค๋ฅผ ์‹œ์ž‘ํ•˜์—ฌ ์ผํšŒ์„ฑ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•˜๋ ค๋ฉด(์˜ˆ: ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ) Intent๋ฅผ startService()์— ์ „๋‹ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. Intent๋Š” ์‹œ์ž‘ํ•  ์„œ๋น„์Šค๋ฅผ ์„ค๋ช…ํ•˜๊ณ  ๋ชจ๋“  ํ•„์ˆ˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
  1. ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ๋Š” ๋ชจ๋“  ์•ฑ์ด ์ˆ˜์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์‹œ์ง€์ž…๋‹ˆ๋‹ค. ์‹œ์Šคํ…œ์€ ์‹œ์Šคํ…œ์ด ๋ถ€ํŒ…๋  ๋•Œ ๋˜๋Š” ๊ธฐ๊ธฐ๊ฐ€ ์ถฉ์ „์„ ์‹œ์ž‘ํ•  ๋•Œ ๋“ฑ ์‹œ์Šคํ…œ ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. Intent๋ฅผ sendBroadcast() ๋˜๋Š” sendOrderedBroadcast()์— ์ „๋‹ฌํ•˜๋ฉด ๋‹ค๋ฅธ ์•ฑ์— ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ์—์„œ Intent์˜ ์‚ฌ์šฉ

MainActivity.kt

 val intent = Intent(this, ResultActivity::class.java)
 intent.putExtra("height", height) //"height"์ด key๊ฐ’
 intent.putExtra("weight", weight) //"weight"์ด key๊ฐ’
 startActivity(intent)

ResultActivity.kt

 val height = intent.getIntExtra("height", 0) //key๊ฐ’์„ ๋ฐ›์•„์™€์„œ height์— ์ €์žฅ
 val weight = intent.getIntExtra("weight", 0) //key๊ฐ’์„ ๋ฐ›์•„์™€์„œ weight์— ์ €์žฅ

๐Ÿ˜€๋งˆ๋ฌด๋ฆฌ

Intent์˜ ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ ์‚ฌ๋ก€์—์„œ 1๋ฒˆ์€ ๊ฐ•์˜ ๋“ค์œผ๋ฉด์„œ ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ• ๊ฐ™์€๋ฐ 2,3๋ฒˆ์€ ์ž์„ธํžˆ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ๋‚ด์ผ ๋‹ค์‹œ ์•Œ์•„๋ด์•ผ๊ฒ ๋‹ค!

profile
๋‚ด๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์“ฐ๋Š” ๋ธ”๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€