registerForActivityResultで「attempting to register while current state is RESUMED. LifecycleOwners must call register before they are STARTED.」

「AndroidX Activity 1.2.0-alpha02 と Fragment 1.3.0-alpha02」で導入された`registerForActivityResult`を使ってみようと思って、実装してみました。

```build.gradle
plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

android {
    ...
    buildFeatures {
        dataBinding true
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation "androidx.activity:activity-ktx:1.2.2"

    testImplementation 'junit:junit:4.+'

    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
```

```MainActivity.kt
class MainActivity : AppCompatActivity() {

    private val binding: ActivityMainBinding by lazy {
        DataBindingUtil.setContentView(this, R.layout.activity_main)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding.button.setOnClickListener {
            clickButton()
        }
    }

    private fun clickButton() {

        val startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            Toast.makeText(this, "SubActivityから戻ってきた", Toast.LENGTH_SHORT).show()
        }

        startForResult.launch(Intent(this, SubActivity::class.java))
    }
}
```

実行してボタンをクリックすると、

```
java.lang.IllegalStateException: 
LifecycleOwner XXXX is attempting to register while current state is RESUMED. 
LifecycleOwners must call register before they are STARTED.
```

というエラーでクラッシュした。

いろいろ調べてみると、`registerForActivityResult`をフィールドとして定義しないとダメっぽい。

修正↓

```kotlin
class MainActivity : AppCompatActivity() {

    // 修正箇所
    private val startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
        Toast.makeText(this, "SubActivityから戻ってきた", Toast.LENGTH_SHORT).show()
    }

    private val binding: ActivityMainBinding by lazy {
        DataBindingUtil.setContentView(this, R.layout.activity_main)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding.button.setOnClickListener {
            clickButton()
        }
    }

    private fun clickButton() {
        startForResult.launch(Intent(this, SubActivity::class.java))
    }
}
```

これでよしと思ったら、またクラッシュ。

再度調べてみると`build.gradle`に`implementation "androidx.fragment:fragment-ktx:1.3.2"`が足りなかった。（Fragment作らないから`androidx.activity:activity-ktx:1.2.2`だけで良いかと思ってた...）

修正↓

```kotlin
dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation "androidx.activity:activity-ktx:1.2.2"
    // 追加
    implementation "androidx.fragment:fragment-ktx:1.3.2"

    testImplementation 'junit:junit:4.+'

    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
```