コンテンツにスキップ

registerForActivityResult利用時にIllegalStateException

AndroidでActivityResultApi1registerForActivityResultを使う際、下記の実行時例外が発生したのでその対処方です。

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

環境

  • compileSdkVersion 30
  • androidx.activity:activity-ktx 1.2.4
  • androidx.fragment:fragment-ktx 1.3.6

発生条件

エラーメッセージの通り、STARTEDより後にregisterForActivityResultを呼び出すとダメです。例えばボタンのクリック時なんかに呼び出そうとすると、クリック時のActivityのlifecycleはRESUMEDなので冒頭のようなエラーメッセージが出ます。

class FooActivity: AppCompatActivity{

  //  ...

  override fun onCreate(){
    super.onCreate()

    findViewById<Button>(R.id.button_bar).also{ button ->
      button.setOnClickListener {
        //  ここが実行されるのはRESUMEDのとき
        registerForActivityResult(StartActivityForResult()){ result ->
          //  handle result
        }.run{
          launch(Intent(this@FooActivity, ReturnResultActivity::class.java))
        }
      }
    }
  }
}

対応方法

エラーメッセージによるとlifecycleOwnerの登録はSTARTEDより前に済ませてくださいねーって書いてあるので、ActivityResultLauncherをクラスフィールドに出してあげます。 エラーメッセージが親切ですね。

class FooActivity: AppCompatActivity{

  //  クラスフィールドとしてActivityResultLauncherを出してあげる
  val launcher = registerForActivityResult(StartActivityForResult()){ result ->
    //  handle result
  }

  override fun onCreate(){
    super.onCreate()

    findViewById<Button>(R.id.button_bar).also{ button ->
      button.setOnClickListener {
        launcher.launch(Intent(this@FooActivity, ReturnResultActivity::class.java))
      }
    }
  }
}

最終更新日: August 9, 2021