Upgrade to Pro — share decks privately, control downloads, hide ads and more …

startActivityForResult()とonActivityResult()がDep...

Sansan
June 20, 2020

startActivityForResult()とonActivityResult()がDeprecatedになった対処法 / startActivityForResult() and onActivityResult() has Deprecated

■イベント 

Sansan Android Engineer's Talk【オンライン開催】

https://sansan.connpass.com/event/176806/

■登壇概要
タイトル:startActivityForResult()とonActivityResult()がDeprecatedになった対処法
発表者:Sansan事業部 プロダクト開発部 古川真次

▼Sansan Builders Box
https://buildersbox.corp-sansan.com/

Sansan

June 20, 2020
Tweet

More Decks by Sansan

Other Decks in Technology

Transcript

  1. 自己紹介 • 古川 真次(ふるしん) •  @furusin_oriver • Sansan,Inc. Android Engineer

    • 大阪府在住 • 趣味:勉強会の主催 ◦ GDG Osaka オーガナイザー ◦ Kansai.kt オーガナイザー
  2. 対応方法:startActivityForResult()とonActivityResult() • 従来の書き方 navigationButton.setOnClickListener { startActivityForResult(SubActivity.createIntent(this), REQUEST_CODE) } override fun

    onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (resultCode == Activity.RESULT_OK) { when (requestCode) { REQUEST_CODE -> {} // do something else -> {} // do nothing } } }
  3. 対応方法:startActivityForResult()とonActivityResult() • こう変わります navigationButton.setOnClickListener { val startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {

    result: ActivityResult? -> when (result?.resultCode) { Activity.RESULT_OK -> {} // do something else -> {} // do something } } startForResult.launch(SubActivity.createIntent(this)) }
  4. 対応方法:startActivityForResult()とonActivityResult() • こう変わります① navigationButton.setOnClickListener { val startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {

    result: ActivityResult? -> when (result?.resultCode) { Activity.RESULT_OK -> {} // do something else -> {} // do something } } startForResult.launch(SubActivity.createIntent(this)) } onActivityResult() に代わるようなもの。
  5. • こう変わります② navigationButton.setOnClickListener { val startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result:

    ActivityResult? -> when (result?.resultCode) { Activity.RESULT_OK -> {} // do something else -> {} // do something } } startForResult.launch(SubActivity.createIntent(this)) } 対応方法:startActivityForResult()とonActivityResult() startActivityForResult() に代わるもの。REQUEST_CODEがなくなりました
  6. 何が嬉しいか • リクエスト/レスポンスが1箇所にまとまる • REQUEST_CODEがない分明確に、シンプルになる navigationButton.setOnClickListener { val startForResult =

    registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult? -> when (result?.resultCode) { Activity.RESULT_OK -> {} // do something else -> {} // do something } } startForResult.launch(SubActivity.createIntent(this)) }
  7. Android Developers > Jetpack > Libraries > Activity https://developer.android.com/jetpack/androidx/releases/activity#1.2.0-alpha03 Getting

    a result from an activity https://developer.android.com/training/basics/intents/result サンプルコード https://github.com/furusin/ActivityResultApiSample 参考文献