[Android] startActivityForResult deprecated kotlin

[Android] startActivityForResult deprecated kotlin

728x90

1. 배경

startActivityForeResult가 deprecated 되었다.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) 의 복잡도가 높아진다는 이유로..

2. 해결방안.

활동에서 결과 가져오기[2] 제일 아래 하단을 보면, 쉽게 알수 있다.

- 자바

ActivityResultLauncher mStartForResult = registerForActivityResult(new StartActivityForResult(), new ActivityResultCallback() { @Override public void onActivityResult(ActivityResult result) { if (result.getResultCode() == Activity.RESULT_OK) { Intent intent = result.getData(); // Handle the Intent } } }); @Override public void onCreate(@Nullable savedInstanceState: Bundle) { // ... Button startButton = findViewById(R.id.start_button); startButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { // The launcher with the Intent you want to start mStartForResult.launch(new Intent(this, ResultProducingActivity.class)); } }); }

-코틀린

val startForResult = registerForActivityResult(StartActivityForResult()) { result: ActivityResult -> if (result.resultCode == Activity.RESULT_OK) { val intent = result.data // Handle the Intent } } override fun onCreate(savedInstanceState: Bundle) { // ... val startButton = findViewById(R.id.start_button) startButton.setOnClickListener { // Use the Kotlin extension in activity-ktx // passing it the Intent you want to start startForResult.launch(Intent(this, ResultProducingActivity::class.java)) } }

- 리절트

val sendIntent = Intent() sendIntent.putExtra("callback", "테스트") sendIntent.putExtra("menu", "어디로이동") activity?.setResult(Activity.RESULT_OK, sendIntent)

참고자료:

[1] startActivityForResult deprecated java,https://blog.daum.net/gomahaera/26

[2] 활동에서 결과 가져오기, https://developer.android.com/training/basics/intents/result?hl=ko#java

728x90

from http://helloit.tistory.com/333 by ccl(A) rewrite - 2021-11-10 11:01:49