[Android] Intent ์ •๋ฆฌ

Minjun Kimยท2023๋…„ 8์›” 29์ผ
0

Android

๋ชฉ๋ก ๋ณด๊ธฐ
26/47
post-thumbnail

๐Ÿ“ SeSAC์˜ 'JetPack๊ณผ Kotlin์„ ํ™œ์šฉํ•œ Android App ๊ฐœ๋ฐœ' ๊ฐ•์ขŒ๋ฅผ ์ •๋ฆฌํ•œ ๊ธ€ ์ž…๋‹ˆ๋‹ค.


๐Ÿ“‡ Intent ๊ฐœ์š”

๐Ÿ“š Component

์•ˆ๋“œ๋กœ์ด๋“œ์—๋Š” ์ปดํฌ๋„ŒํŠธ ๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค.

โ“ ์ปดํฌ๋„ŒํŠธ ๋ž€?

์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ตฌ์„ฑ์š”์†Œ

์šฐ๋ฆฌ๊ฐ€ ํ•˜๋‚˜์˜ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค๋ฉด, ์ด ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“ค์–ด ๋†“์€ ๋ช‡๋ช‡ ์ปดํฌ๋„ŒํŠธ๋กœ ๊ตฌ์„ฑ์ด ๋œ๋‹ค. ์ฆ‰, ์ปดํฌ๋„ŒํŠธ ๋Š” ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ตฌ์„ฑ์š”์†Œ๊ฐ€ ๋œ๋‹ค.

โ“ ์ปดํฌ๋„ŒํŠธ์˜ ๊ฐœ๋ฐœ ๋ชจ์Šต

๊ทธ๋Ÿผ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‹ค์ œ ์–ด๋–ค ๋ชจ์Šต์œผ๋กœ ๊ฐœ๋ฐœ ๋ ๊นŒ?

ํด๋ž˜์Šค ๋กœ ๊ฐœ๋ฐœ๋œ๋‹ค. ๊ฐœ๋ฐœ์ž๊ฐ€ ๋งŒ๋“œ๋Š” ํด๋ž˜์Šค๊ฐ€ ๊ณง ์ปดํฌ๋„ŒํŠธ ์ธ ๊ฒƒ์ด๋‹ค.

๊ทธ๋Ÿผ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ๋ชจ๋“  ํด๋ž˜์Šค๊ฐ€ ์ปดํฌ๋„ŒํŠธ ๊ฐ€ ๋˜๋Š” ๊ฑธ๊นŒ?
-> ์•„๋‹ˆ๋‹ค.

์ปดํฌ๋„ŒํŠธ ํด๋ž˜์Šค ์™€ ์ผ๋ฐ˜ ํด๋ž˜์Šค ๋กœ ๊ตฌ๋ถ„๋œ๋‹ค. ์ด ๋‘˜์˜ ๊ตฌ๋ถ„ ๊ธฐ์ค€์€ ๋ผ์ดํ”„์‚ฌ์ดํด ์ด๋‹ค.

์ปดํฌ๋„ŒํŠธ ํด๋ž˜์Šค ์˜ ๋ผ์ดํ”„์‚ฌ์ดํด์€ ์‹œ์Šคํ…œ์—์„œ ๊ด€๋ฆฌํ•œ๋‹ค.

์ผ๋ฐ˜ ํด๋ž˜์Šค ์˜ ๋ผ์ดํ”„์‚ฌ์ดํด์€ ๊ฐœ๋ฐœ์ž ์ฝ”๋“œ์—์„œ ๊ด€๋ฆฌํ•œ๋‹ค.

๊ฐ„๋‹จํ•˜๊ฒŒ ๋งํ•˜๋ฉด, ๊ฐœ๋ฐœ์ž ์ฝ”๋“œ์—์„œ ๊ฐ์ฒด ์ƒ์„ฑ์ด ๋˜๋Š” ํด๋ž˜์Šค๋Š” ์ผ๋ฐ˜ ํด๋ž˜์Šค์ด๋‹ค.

๐Ÿ“Œ Component ์ข…๋ฅ˜

์ปดํฌ๋„ŒํŠธ์—๋Š” 4์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค.

Activity
- ํ™”๋ฉด ์ถœ๋ ฅ ๋Šฅ๋ ฅ
Service
- ์žฅ์‹œ๊ฐ„์˜ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—… ๋Šฅ๋ ฅ
BroadcastReceiver
- ์ด๋ฒคํŠธ ๋ชจ๋ธ๋กœ ์‹คํ–‰๋˜๋Š” ์ปดํฌ๋„ŒํŠธ (์—ฌ๊ธฐ์„œ ์ด๋ฒคํŠธ ๋ชจ๋ธ์ด๋ž€ ์‹œ์Šคํ…œ ์ด๋ฒคํŠธ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.)
ContentProvider
- ์•ฑ ๊ฐ„์˜ ๋ฐ์ดํ„ฐ ๊ณต์œ  ๋Šฅ๋ ฅ

  • ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‚ด์—์„œ ์ปดํฌ๋„ŒํŠธ ๊ตฌ์„ฑ์€ ๊ฐœ๋ฐœ์ž ์„ ํƒ ์‚ฌํ•ญ์ด๋‹ค.

  • A ์ฒ˜๋Ÿผ ํ•˜๋‚˜์˜ ํ™”๋ฉด ์ถœ๋ ฅ๋งŒ ํ•  ์ˆ˜๋„ ์žˆ๊ณ  B ์ฒ˜๋Ÿผ 3๊ฐœ์˜ ํ™”๋ฉด ์ถœ๋ ฅ๋งŒ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

  • ๊ทธ๋ฆฌ๊ณ  ์ปดํฌ๋„ŒํŠธ ๊ตฌ์„ฑ์œผ๋กœ ์–ด๋–ค ๊ธฐ๋Šฅ์„ ๊ฐ–์ถ˜ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ธ์ง€๋„ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค.
    C ๋Š” ํ™”๋ฉด 1๊ฐœ์™€ ๋ฐ์ดํ„ฐ ๊ณต์œ , D๋Š” ํ™”๋ฉด 1๊ฐœ์™€ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—… ๊ธฐ๋Šฅ์œผ๋กœ ๊ฐœ๋ฐœ๋˜์—ˆ๋‹ค.

๐Ÿ“š Intent

๊ทธ๋Ÿผ Compnent๋Š” Intent์™€ ๋ฌด์Šจ ๊ด€๊ณ„๊ฐ€ ์žˆ๋Š” ๊ฑธ๊นŒ?

์ปดํฌ๋„ŒํŠธ๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์•„ํ‚คํ…์ฒ˜ ์š”์†Œ์ด๊ณ , ๊ทธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•œ API ์ ์ธ ํด๋ž˜์Šค๊ฐ€ ๋ฐ”๋กœ ์ธํ…ํŠธ๋‹ค.

โ“ Intent ๋ž€?

์ปดํฌ๋„ŒํŠธ๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์‹œ์Šคํ…œ์— ๋„์šฐ๋Š” ๋ฉ”์‹œ์ง€

์ธํ…ํŠธ๋ผ๋Š” ํด๋ž˜์Šค๋Š” ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ํด๋ž˜์Šค์ด๋‹ค. ํ”ํžˆ VOํด๋ž˜์Šค๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์–ด๋–ค ์—…๋ฌด๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜๋„ ์—†๊ณ  ๋ช‡๋ช‡ ์ •๋ณด๋งŒ ๋‹ด๊ธด๋‹ค.
๊ทธ๋ž˜์„œ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ๋‹จ์ˆœํžˆ ์ธํ…ํŠธ๋ฅผ ํ™”๋ฉด ์ „ํ™˜ API ๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ๋‚˜๋„ ์ง€๊ธˆ ์•Œ์•˜๋‹ค

ํ•˜์ง€๋งŒ ์ธํ…ํŠธ๋ž€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์™„์„ฑํ•˜๊ธฐ ์œ„ํ•œ ํ•ต์‹ฌ ํด๋ž˜์Šค์ด๋‹ค.
๋•Œ๋ฌธ์— ์‹œ์Šคํ…œ์— ์ธํ…ํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์‹œ์Šคํ…œ์—์„œ ํ•ด๋‹น ์ธํ…ํŠธ๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด ์ค˜์•ผ ๋˜๋Š”์ง€,
์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด ์ฃผ๋Š”์ง€์— ๋Œ€ํ•œ ๋‚ด๋ถ€ ๋งค์ปค๋‹ˆ์ฆ˜์„ ์ •ํ™•ํ•˜๊ฒŒ ์ดํ•ดํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.

๊ฒฐ๋ก ์ ์œผ๋กœ Intent ๋ž€ ๊ทธ๋ƒฅ ๋ฉ”์‹œ์ง€๊ฐ€ ๋‹ด๊ธด VOํด๋ž˜์Šค์ด๊ณ ,
๊ทธ ๋ฉ”์‹œ์ง€๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‹คํ–‰ ์‹œํ‚ค๊ธฐ ์œ„ํ•œ ๋ฉ”์‹œ์ง€์ด๋‹ค.

  • MainActivity ์—์„œ ์ง์ ‘ DetailActivity ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํ™”๋ฉด ์ „ํ™˜์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
    ๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ ํ™”๋ฉด ์ „ํ™˜์„ ํ•  ์ˆ˜ ์žˆ์„๊นŒ?

  • ์‹œ์Šคํ…œ์— ์ธํ…ํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ  -> ์‹œ์Šคํ…œ์—์„œ ๋ฐœ์ƒํ•œ ์ธํ…ํŠธ์˜ ์ •๋ณด๋ฅผ ๋ถ„์„ํ•˜์—ฌ -> ์‹œ์Šคํ…œ์—์„œ DetailActivity๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค.

  • ์ด ํ”„๋กœ์„ธ์Šค๋Š” ์™ธ๋ถ€ ์•ฑ์˜ ๊ฒฝ์šฐ์—๋„ ๋™์ผํ•˜๋‹ค.
    ์‹œ์Šคํ…œ์— ์ธํ…ํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ  ๊ทธ ์ธํ…ํŠธ๊ฐ€ ์™ธ๋ถ€ ์•ฑ์˜ ํŠน์ • ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ๊ตฌ์กฐ๋‹ค.

๐Ÿ“‡ Activity ์‹คํ–‰

val intent = Intent(this, DetailActivity::class.java)
startActivity(intent)
  • public void startActivity (Intent intent)

  • ์ธํ…ํŠธ์— context ์™€ ํ™”๋ฉด ์ „ํ™˜ํ•  ์•กํ‹ฐ๋น„ํ‹ฐ์˜ ์ •๋ณด๋ฅผ ๋‹ด๋Š”๋‹ค.

  • ๊ทธ๋ฆฌ๊ณ  startActivity ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ์‹œ์Šคํ…œ์— ๋„์šด๋‹ค.


๐Ÿ“‡ Extra Data

Intent ๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•

๋‹ค๋ฅธ ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ ๋ฐ์ดํ„ฐ๋„ ๊ฐ™์ด ์ „๋‹คํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ?

์˜ˆ๋ฅผ ๋“ค์–ด, MainActivity์˜ EditText์— ์ด๋ฆ„์„ ์ ๊ณ  ์ด๋ฅผ DetailActivity์˜ TextView์— ์ „๋‹ฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?
-> Intent ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

์ธํ…ํŠธ๋Š” ์™ธ๋ถ€ ์•ฑ์˜ ํŠน์ • ์ปดํฌ๋„ŒํŠธ๋„ ์‹คํ–‰์‹œํ‚ค๋‹ค. ์ฆ‰, Intent๋Š” ์ธํ…ํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ ์ปดํฌ๋„ŒํŠธ์™€ ์ธํ…ํŠธ์— ์˜ํ•ด ์‹คํ–‰๋˜๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ณตํ†ต์œผ๋กœ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด๋‹ค.

๐Ÿ“Œ Extra Data ์ „๋‹ฌ

intent.putExtra("id", i)
intent.putExtra("title", datas[i]
  • public Intent putExtra (String name, CharSequence value)

๐Ÿ“Œ Extra Data ์ˆ˜์‹ 

val intent = intent
  • ์ž์‹ ์„ ์‹คํ–‰์‹œํ‚จ ์ธํ…ํŠธ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฝ‘์•„์™€์•ผ ํ•œ๋‹ค.

  • ์ด๋ฅผ ์•กํ‹ฐ๋น„ํ‹ฐ์—์„œ intent๋ž€ ํ”„๋กœํผํ‹ฐ๋กœ ์ œ๊ณตํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ๊ทธ๋ƒฅ ๊ฐ–๋‹ค ์“ฐ๋ฉด ๋œ๋‹ค.

getIntExtra (String name, int defaultValue)

getStringExtra (String name)

getDoubleExtra (String name, double defaultValue)
  • ๊ทธ๋ฆฌ๊ณ  ์•Œ๋งž๋Š” Extra Data ํƒ€์ž…์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์ฃผ๋ฉด ๋œ๋‹ค.

  • defaultValue ๋Š” ํ‚ค๊ฐ’์ด ์•ˆ ๋„˜์–ด์™”์„ ๋•Œ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.


๐Ÿ“‡ Activity Result

Intent ๋กœ ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋˜๋Œ๋ ค ๋ฐ›๋Š” ๋ฐฉ๋ฒ•์€ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

startActivityForResult() ํ•จ์ˆ˜์™€ ActivityResultLauncher ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

โ“ ์™œ ๋‘ ๊ฐ€์ง€ ์ผ๊นŒ?

startActivityForResult() ๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ 11 ๋ฒ„์ „์—์„œ deprecated ๋˜์—ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ActivityResultLauncher ๋ผ๋Š” ๋ฐฉ๋ฒ•์ด ์ƒˆ๋กญ๊ฒŒ ์ œ์‹œ ๋˜์—ˆ๋‹ค.

์•ˆ๋“œ๋กœ์ด๋“œ 11 ๋ฒ„์ „ ๊ณต๊ฐœ ์ผ์ž๊ฐ€ 2020๋…„ 9์›” 8์ผ์ด๋‹ค. ์ฆ‰, ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• ๋ชจ๋‘ ์•Œ์•„์•ผ ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.

๋‘˜ ๋‹ค ์‚ฌํ›„์ฒ˜๋ฆฌ๊ฐ€ ๋ชฉ์ ์ด๋ผ๋Š” ๊ฒƒ์€ ๋ณ€ํ•จ์—†๋‹ค.

๐Ÿ“š startActivityForResult

val intent = Intent(this, DetailActivity::class.java)
startActivityForResult(intent, 10)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
	super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == 10 && resultCode == RESULT_OK) {
   		//...........
    }
}
  • ์‚ฌํ›„์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด startActivityForResult() ํ•จ์ˆ˜๋กœ ์ธํ…ํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

  • onActivityResult() ํ•จ์ˆ˜๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ๋ฐ›์•„ ๋†“์œผ๋ฉด,
    startActivityForResult() ๋ฅผ ์ž‘์„ฑํ•œ ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ๋‹ค์‹œ ํ™”๋ฉด์— ๋œฐ ๋•Œ ์ž๋™ ์ฝœ ๋œ๋‹ค.

โ— ๋‹ค์ˆ˜์˜ ์‚ฌํ›„์ฒ˜๋ฆฌ

startActivityForResult ๋ฅผ ํ•œ ๋ฒˆ๋งŒ ์“ฐ๋ผ๋Š” ๋ฒ•์€ ์—†๋‹ค.
๋‹ค์ˆ˜์˜ ๋‹ค๋ฅธ ์•กํ‹ฐ๋น„ํ‹ฐ์—์„œ ๊ฒฐ๊ณผ๊ฐ’์„ ๋˜๋Œ๋ ค ๋ฐ›์•„์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด๋ผ๋ฉด, ์ด๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์„๊นŒ?
-> startActivityForResult์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ž์ธ ๊ฐœ๋ฐœ์ž ์ˆซ์ž ๊ฐ’, requestCode ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.

ํ™”๋ฉด์ด ๋˜๋Œ์•„์˜ค๋ฉด onActivityResult ํ•จ์ˆ˜๊ฐ€ ์ž๋™ ์ฝœ ๋œ๋‹ค. ์ด ๋•Œ, requestCode๊ฐ€ ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ธํ…ํŠธ์— ์˜ํ•ด ์‹คํ–‰๋œ ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ๊ฒฐ๊ณผ๊ฐ’์„ ๋˜๋Œ๋ฆฌ๊ธฐ ์ „์— ์ƒํƒœ ์ฝ”๋“œ๊ฐ’, resultCode ๋ฅผ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค. ๋ณดํ†ต OK, CANCEL ์„ ๋งŽ์ด ์ค€๋‹ค.

๐Ÿ“š ActivityResultLauncher

์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ณ  ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ํš๋“

ํผ๋ฏธ์…˜ ์กฐ์ • ๊ฒฐ๊ณผ ํš๋“

val requestActivity: ActivityResultLauncher<Intent> =
	registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
		//...............
	}
  • requestActivity ๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›๊ธฐ ์œ„ํ•œ ๋ถ€๋ถ„

  • ์ฝ”๋“œ ๋ธ”๋Ÿญ ์•ˆ์— ๋˜๋Œ์•„์˜จ ๊ฒฐ๊ณผ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

val intent: Intent = Intent(this, DetailActivity::class.java)
requestActivity.launch(intent)
  • ActivityResultLauncher ๊ฐ์ฒด์˜ launcher ํ•จ์ˆ˜๋กœ ์ธํ…ํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

๐Ÿ“Œ ๊ฒฐ๊ณผ๊ฐ’ ์ „๋‹ฌ

๋˜‘๊ฐ™์ด putExtra ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋œ๋‹ค.

intent.putExtra("result", "hello $id")
setResult(RESULT_OK, intent)
finish()
  • Intent ์˜ Extra Data ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ

  • setResult() ๋กœ ์ƒํƒœ ๋“ฑ๋ก, ์š”์ฒญ ์ฒ˜๋ฆฌ ์„ฑ๊ณต ์—ฌ๋ถ€๋ฅผ ์ง€์ •ํ•œ๋‹ค. ( OK or CANCEL )


๐Ÿงฉ ์‹ค์Šต ์˜ˆ์ œ

  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="startActivity" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ActivityResultLauncher" />

</LinearLayout>


  • activity_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".DetailActivity">

    <Button
        android:id="@+id/btn_finish"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="finish" />

</LinearLayout>


  • MainActivity.kt
package com.kotdev99.android.c42

class MainActivity : AppCompatActivity() {
	private lateinit var resultView: TextView

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_main)

		val btn1 = findViewById<Button>(R.id.btn1)
		val btn2 = findViewById<Button>(R.id.btn2)
		resultView = findViewById(R.id.tv_result)

		btn1.setOnClickListener {
			val intent = Intent(this, DetailActivity::class.java)
			intent.putExtra("id", "first")
			startActivityForResult(intent, 10)
		}

		val resultLauncher: ActivityResultLauncher<Intent> = registerForActivityResult(
			ActivityResultContracts.StartActivityForResult()
		) {
			resultView.text = "result: ${it.data?.getStringExtra("result")}"
		}
		btn2.setOnClickListener {
			val intent = Intent(this, DetailActivity::class.java)
			intent.putExtra("id", "second")
			resultLauncher.launch(intent)
		}
	}

	override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
		super.onActivityResult(requestCode, resultCode, data)
		if (requestCode == 10 && resultCode == RESULT_OK) {
			val result: String? = data?.getStringExtra("result")
			resultView.text = "result : $result"
		}
	}
}

  • DetailActivity.kt
package com.kotdev99.android.c42

class DetailActivity : AppCompatActivity() {
	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_detail)

		val id = intent.getStringExtra("id")
		val btn = findViewById<Button>(R.id.btn_finish)
		btn.setOnClickListener {
			intent.putExtra("result", "hello : $id")
			setResult(RESULT_OK, intent)
			finish()
		}
	}
}

๐Ÿ“ฒ ๊ฒฐ๊ณผ


๐Ÿ“‡ Implicit Intent

๐Ÿ“š Intent Filter

์ธํ…ํŠธ๋Š” ๋‘ ๊ฐ€์ง€๋กœ ๋‚˜๋‰œ๋‹ค.

๋ช…์‹œ์  ์ธํ…ํŠธ(Explicit Intent) : class type reference ์ •๋ณด๋ฅผ ํ™œ์šฉํ•œ ์ธํ…ํŠธ

์•”์‹œ์  ์ธํ…ํŠธ(Implicit Intent) : ์ธํ…ํŠธ ํ•„ํ„ฐ ์ •๋ณด๋ฅผ ํ™œ์šฉํ•œ ์ธํ…ํŠธ

ํ•˜๋‚˜์˜ ์•ฑ ์•ˆ์—์„œ ์•กํ‹ฐ๋น„ํ‹ฐ ๊ฐ„์˜ ์ธํ…ํŠธ๋Š” ๋ช…์‹œ์  ์ธํ…ํŠธ๋กœ ์ถฉ๋ถ„ํ•˜๋‹ค.๊ทธ๋Ÿผ ์™ธ๋ถ€ ์•ฑ์˜ ์ปดํฌ๋„ŒํŠธ์— ์ ‘๊ทผ ํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ? ์ด ๋•Œ ์•”์‹œ์  ์ธํ…ํŠธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•˜๋‹ค. Manifest ํŒŒ์ผ์— Intent Filter ๋ฅผ ๊ฑธ์–ด์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.

<activity android:name=".MainActivity"
	<intent-filter>
		<action android:name="ACTION_EDIT" />
	</intent-filter>
</activity>
  • ์ธํ…ํŠธ ํ•„ํ„ฐ์˜ ์ •๋ณด๋Š” ๋ฌธ์ž์—ด ๋งŒ ์ž‘์„ฑ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋•Œ๋ฌธ์— ๋งค์šฐ ์‰ฝ๊ฒŒ ์™ธ๋ถ€์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ“Œ Intent Filter ์ •๋ณด

<intent-filter>
	<action android:name="ACTION_DETAIL" />
	<category android:name="android.intent.category.DEFAULT" />
	<data android:scheme="http" />
</intent-filter>
  • <action> : ์ปดํฌ๋„ŒํŠธ์˜ ๋Šฅ๋ ฅ์„ ํ‘œํ˜„ํ•˜๋Š” ๋ฌธ์ž์—ด

  • <category> : ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์–ด๋Š ๋ฒ”์ฃผ์— ํฌํ•จ๋˜์–ด์•ผ ํ•˜๋Š”์ง€์˜ ๋ฌธ์ž์—ด

  • <data> : ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ์˜ ์ •๋ณด

  • ํ•„ํ„ฐ๋Š” ์ •๋ณด๋Š” ์„ ํƒ ์‚ฌํ•ญ์ด๋‹ค. 3๊ฐœ ๋‹ค ์ž‘์„ฑํ•ด๋„ ๋˜๊ณ  1๊ฐœ๋งŒ ์ž‘์„ฑํ•ด๋„ ๋œ๋‹ค.

๐Ÿ“Œ ์•”์‹œ์  ์ธํ…ํŠธ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

val intent = Intent()
intent.action = "ACTION_EDIT"
intent.data = Uri.parse("http://www.google.com")
startActivity(intent)
  • ์ธํ…ํŠธ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ

  • <action> ๋ฌธ์ž์—ด์ด "ACTION_EDIT" ์ด๊ณ ,

  • <data> ์˜ scheme ์ด "http" ๋กœ ๋˜์–ด์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‹คํ–‰


๐Ÿงฉ ์‹ค์Šต ์˜ˆ์ œ

  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DetailActivity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

  • activity_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DetailActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I am DetailActivity"
        android:textSize="30dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

  • MainActivity.kt
package com.kotdev99.android.c43

class MainActivity : AppCompatActivity() {
	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_main)

		val btn = findViewById<Button>(R.id.btn)
		btn.setOnClickListener {
			val intent = Intent()
			intent.action = "ACTION_DETAIL"
			// category ์ •๋ณด๋ฅผ ์ฃผ์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ์œผ๋กœ DEFAULT ๊ฐ’์ด ๋œ๋‹ค
			intent.data = Uri.parse("http://www.google.com")
			startActivity(intent)
		}
	}
}

๐Ÿ“ฒ ๊ฒฐ๊ณผ

profile
์‘์•  ๋‚˜ ์•„๊ธฐ ๋‰ด๋น„

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