Activity中startActivityForResult和

2022-10-27  本文已影响0人  WangKiven

使用registerForActivityResult

class LaunchActivity : AppCompatActivity() {
    private val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
        if (it.resultCode != Activity.RESULT_OK) finish()
        else findViewById<View>(R.id.toWx).visibility = View.VISIBLE
    }

  ......

  // launcher 使用方法
  launcher.launch(Intent(this, LoginActivity::class.java))
}
class LaunchActivity : AppCompatActivity() {
    private val launcher = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) {
        if (it.resultCode != Activity.RESULT_OK) finish()
        else findViewById<View>(R.id.toWx).visibility = View.VISIBLE
    }

  ......

  // launcher 使用方法。这个就有点麻烦了
  launcher.launch(IntentSenderRequest.Builder(PendingIntent.getActivity(this, 345, Intent(this, LoginActivity::class.java), 0)).build())
  // launcher 删除图片。这个应该才是正常操作吧
  val pi = MediaStore.createTrashRequest(
        contentResolver,
        uris,
        true
   )
   launcher.launch(IntentSenderRequest.Builder(pi.intentSender).build())
}

总结

上一篇 下一篇

猜你喜欢

热点阅读