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

hyihyiยท2023๋…„ 12์›” 19์ผ
0

TIL

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

๐Ÿค”๋‹ค๋ฅธ ์•กํ‹ฐ๋น„ํ‹ฐ๋กœ๋ถ€ํ„ฐ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ์‹์€ ๋ฌด์—‡์ด ์žˆ์„๊นŒ?

startActivityForResult / registerForActivityResult

1. startActivityForResult

: startActivityForResult ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋‹ค๋ฅธ ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์‹œ์ž‘ํ•˜๊ณ  onActivityResult ์ฝœ๋ฐฑ์„ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์ฒ˜๋ฆฌํ•จ

2. registerForActivityResult

: registerForActivityResult ๋ฉ”์†Œ๋“œ์™€ ํ•จ๊ป˜ ActivityResultContract๋ฅผ ์‚ฌ์šฉํ•ด ๊ฒฐ๊ณผ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ActivityResultLauncher๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ด๋ฅผ ์‚ฌ์šฉํ•ด ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์‹œ์ž‘ํ•จ

ํ•˜์ง€๋งŒ ์ง€๊ธˆ startActivityForResult๋Š” depecated๋˜์–ด registerForActivityResult๋กœ ๋Œ€์ฒด

์˜ˆ์‹œ

SignInActivity : ๋กœ๊ทธ์ธ ํ™”๋ฉด
SignUpActivity : ํšŒ์›๊ฐ€์ž… ํ™”๋ฉด

  1. ๋กœ๊ทธ์ธ ํ™”๋ฉด์—์„œ ํšŒ์›๊ฐ€์ž… ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ํšŒ์›๊ฐ€์ž… ํ™”๋ฉด์œผ๋กœ ์ด๋™
  2. ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ํšŒ์›๊ฐ€์ž… ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด
  3. ๋กœ๊ทธ์ธ ํ™”๋ฉด์„ ํ˜ธ์ถœํ•˜๊ณ  ํšŒ์›๊ฐ€์ž… ํ™”๋ฉด์—์„œ ๋ณด๋‚ธ ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ฐ’์„ ๋กœ๊ทธ์ธํ™”๋ฉด์œผ๋กœ ๋‹ค์‹œ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ์Œ

1. ๋กœ๊ทธ์ธ ํ™”๋ฉด์—์„œ ํšŒ์›๊ฐ€์ž… ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ํšŒ์›๊ฐ€์ž… ํ™”๋ฉด์œผ๋กœ ์ด๋™


2. ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ํšŒ์›๊ฐ€์ž… ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด


3. ํšŒ์›๊ฐ€์ž… ํ™”๋ฉด์—์„œ ๋ณด๋‚ธ ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ฐ’์„ ๋กœ๊ทธ์ธ ํ™”๋ฉด์œผ๋กœ ๋‹ค์‹œ ๋ฐ›์•„์™€ ์ž๋™์œผ๋กœ ์ž…๋ ฅ๋จ


๐Ÿ’ป์ฝ”๋“œ

<SignInActivity.kt>

class SignInActivity : AppCompatActivity() {

    private lateinit var idText: EditText
    private lateinit var passwordText: EditText

	// [1] ActivityResultLauncher ์ดˆ๊ธฐํ™”
    private val signUpActivityResultLauncher = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) { result -> // [2]
        if (result.resultCode == Activity.RESULT_OK) {
            val data = result.data
            data?.let { //data๊ฐ€ null์ด ์•„๋‹ ๋•Œ ์‹คํ–‰
                val userPassword = it.getStringExtra("userPassword") ?: ""
                val userId = it.getStringExtra("userId") ?: ""

                idText.setText(userId)
                passwordText.setText(userPassword)
            }
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sign_in)

        idText = findViewById(R.id.signInIdEditText)
        passwordText = findViewById(R.id.signInPasswordEditText)
        val loginBtn = findViewById<Button>(R.id.signInLoginBtn)
        val signupBtn = findViewById<Button>(R.id.signInSignUpBtn)

        signupBtn.setOnClickListener {
            val intent = Intent(this, SignUpActivity::class.java)
            signUpActivityResultLauncher.launch(intent)
        }
    }
}

[1] signUpActivityResultLauncher๋Š” ActivityResultLauncher ๊ฐ์ฒด
[2] result๋Š” ActivityResult ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ณ€์ˆ˜ ์ด๋ฆ„์œผ๋กœ, ์—ฌ๊ธฐ์„œ resultCode์™€ data๋ฅผ ํ†ตํ•ด ์•กํ‹ฐ๋น„ํ‹ฐ ๊ฒฐ๊ณผ๋ฅผ ์ฒ˜๋ฆฌ

<SignUpActivity.kt>

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

        val nameEditText = findViewById<EditText>(R.id.signUpNameEditText)
        val idEditText = findViewById<EditText>(R.id.signUpIdEditText)
        val passwordEditText = findViewById<EditText>(R.id.signUpPasswordEditText)
        val signupBtn = findViewById<Button>(R.id.SignUpSignUpBtn)

        signupBtn.setOnClickListener {

            val userName = nameEditText.text.toString()
            val userId = idEditText.text.toString()
            val userPassword = passwordEditText.text.toString()

			// [1]
            val resultIntent = Intent().apply {
                putExtra("userName", userName)
                putExtra("userId", userId)
                putExtra("userPassword", userPassword)
            }
            setResult(Activity.RESULT_OK, resultIntent)
            finish() 
        }
    }
}

[1] apply๋กœ Intent ๊ฐ์ฒด์— ๊ฐ ํ‚ค์— ๋งž๋Š” ๋ฐ์ดํ„ฐ ์‚ฝ์ž…


๐Ÿค” startActivity(intent)๋ฅผ ์“ฐ๋ฉด ๋˜๋Š”๋ฐ ์™œ setResult(registerForActivityResult)๋ฅผ ์“ธ๊นŒ?

startActivity

: ๋กœ๊ทธ์ธ ํ™”๋ฉด(1) -> ํšŒ์›๊ฐ€์ž… ํ™”๋ฉด(2) -> ๋กœ๊ทธ์ธ ํ™”๋ฉด(3)
์œ„์˜ ๊ณผ์ •์„ ๊ฑฐ์ณค์„ ๋•Œ 3ํ™”๋ฉด์ด ์ƒˆ๋กœ ์ƒ์„ฑ๋˜์–ด์„œ 1,3์€ ๋‹ค๋ฅธ ํ™”๋ฉด์ด ๋œ๋‹ค.

์ด ์•กํ‹ฐ๋น„ํ‹ฐ์˜ ์ƒ๋ช… ์ฃผ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์œผ๋ฉด AndroidManifest.xml์˜ android:launchMode๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

setResult

: ๋กœ๊ทธ์ธ ํ™”๋ฉด(1) -> ํšŒ์›๊ฐ€์ž… ํ™”๋ฉด(2) -> ๋กœ๊ทธ์ธ ํ™”๋ฉด(1)
๊ธฐ์กด์— ์žˆ๋˜ ์•กํ‹ฐ๋น„ํ‹ฐ์— ๊ฒฐ๊ณผ๊ฐ’ ๋Œ๋ ค์คŒ

ex) ์˜ˆ์‹œ
1) ํ™”๋ฉด ์ „ํ™˜์ด ๋๋‹ค๊ฐ€ ๋‹ค์‹œ ์ด์ „ ํ™”๋ฉด์œผ๋กœ ๋Œ์•„์™”์„ ๋•Œ 
์Šคํฌ๋กค ์œ„์น˜ ๊ฐ™์€ ๊ฒƒ๋“ค์„ ์œ ์ง€ํ•ด์•ผํ•  ๋•Œ ์‚ฌ์šฉ 

2) ํšŒ์›๊ฐ€์ž… ๋„์ค‘ ์ฃผ์†Œ ์ž…๋ ฅ ์ฐฝ์„ ๋„์› ๋‹ค๊ฐ€ ๋‹ค์‹œ ์ด์ „ ํ™”๋ฉด์œผ๋กœ ๋Œ์•„์™”์„ ๋•Œ 
์ž…๋ ฅํ•ด๋’€๋˜ ์ •๋ณด๋“ค์„ ๊ทธ๋Œ€๋กœ ๋ณด์กดํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ
startActivity๋ณด๋‹ค ํ›จ์”ฌ ๋‚ซ๋‹ค

๋ฒˆ์™ธ

์ฝ”๋“œ๋ฅผ ์งœ๋ฉด์„œ let, apply์— ๋Œ€ํ•ด ๋‹ค์‹œ ๊ณต๋ถ€ํ–ˆ๋‹ค.
๋‚˜๋Š” ์–ด๋–ค ๊ฐœ๋…์„ ๊ณต๋ถ€ํ•  ๋•Œ ๋” ํŽธ๋ฆฌํ•˜๋‹ค๊ณ  ํ•ด๋„ ์–ด๋Š ๋ถ€๋ถ„ ๋•Œ๋ฌธ์— ํŽธ๋ฆฌํ•œ์ง€์™€ ์™œ ์จ์•ผํ•˜๋Š”์ง€๋ฅผ ๋ชจ๋ฅด๊ฒ ์œผ๋ฉด ์ดํ•ด๊ฐ€ ์•ˆ๋๋‹ค. ๊ทธ๋ž˜์„œ let๊ณผ apply๋ฅผ ์ผ์„ ๋•Œ ์–ด๋–ค ์ ์ด ํŽธ๋ฆฌํ•œ์ง€ ์ ์–ด๋ดค๋‹ค.

  • ์œ„์˜ ์ฝ”๋“œ์—์„œ let์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ
data?.let { data -> //data๊ฐ€ null์ด ์•„๋‹ ๋•Œ ์‹คํ–‰
   val userId = data.getStringExtra("userId") ?: ""
   val userPassword = data.getStringExtra("userPassword") ?: ""
   idText.setText(userId)
   passwordText.setText(userPassword)
}
  • let์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์„ ๋•Œ
if (data != null) { // data๊ฐ€ null์ด ์•„๋‹ ๋•Œ ์‹คํ–‰
    val userId = data.getStringExtra("userId") ?: ""
    val userPassword = data.getStringExtra("userPassword") ?: ""
    idText.setText(userId)
    passwordText.setText(userPassword)
}

  • ์œ„์˜ ์ฝ”๋“œ์—์„œ apply๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ
val resultIntent = Intent().apply {
    putExtra("userName", userName)
    putExtra("userId", userId)
    putExtra("userPassword", userPassword)
  • ์œ„์˜ ์ฝ”๋“œ์—์„œ apply๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์„ ๋•Œ
val resultIntent = Intent()
resultIntent.putExtra("userName", userName)
resultIntent.putExtra("userId", userId)
resultIntent.putExtra("userPassword", userPassword)
profile
๋‚ด๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์“ฐ๋Š” ๋ธ”๋กœ๊ทธ

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