κ²°κ³Όλ₯Ό μν νλμ μμν λ λ©λͺ¨λ¦¬ λΆμ‘±μΌλ‘ νλ‘μΈμ€μ νλμ΄ μλ©Έλ μ μμ΅λλ€. νΉν μΉ΄λ©λΌ μ¬μ©κ³Ό κ°μ΄ λ©λͺ¨λ¦¬λ₯Ό λ§μ΄ μ¬μ©νλ μμ μ κ²½μ°μλ μλ©Έλ νλ₯ μ΄ λ§€μ° λμ΅λλ€.
κΈ°μ‘΄μ μ¬μ©νλ startAcitivityForResult λ deprecated κ° λμλ€.
곡μ λ¬Έμμ λ΄μ©μ νμΈν΄λ³΄λ©΄ λ©λͺ¨λ¦¬ λΆμ‘±μΌλ‘ νλ‘μΈμ€μ νλμ΄ μλ©Έλ μ μλ μ΄μ λΌκ³ νλ€.
ComponentActivity λλ Fragmentμ μμ λ, Activity Result APIμμ μ 곡νλ registerForActivityResult() APIλ₯Ό ν΅ν΄ κ²°κ³Ό μ½λ°±μ λ±λ‘ν μ μλ€.
곡μλ¬Έμμ λ°λ₯΄λ©΄ startActivityForResult κ° deprecated λκ³ , Activity callback μ μν΄ registerForActivityResult μ μ¬μ©μ κΆμ₯νκ³ μλ€.
μμ λ₯Ό ν΅ν΄ registerForActivityResult μ μ¬μ©λ²μ μμ보μ.
https://developer.android.com/training/basics/intents/result?hl=ko
val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
// call back event
if (result.resultCode == RESULT_OK){
val intent = result.data
// Handle the Intent (callback)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
// ...
binding.selectButton.setOnClickListener {
val intent = Intent(this, SubActivity::class.java)
launcher.launch(intent)
}
}
// νλ©΄ μ’
λ£ μ
val intent = Intent(this, MainActivity::class.java)
intent.putExtra("message", "λ³΄λΌ λ©μμ§")
setResult(RESULT_OK, intent)
finish()
μ’μ κΈ κ°μ¬ν©λλ€. μμ£Ό μ¬κ²μ :)