如何为我的 activity 设置不同条件下的请求代码以绕过 onActivityResult?

How can I set a request code under different conditions for my activity to bypass onActivityResult?

我有一个程序可以让我在使用 android 系统相机 activity 拍摄照片的过程中存储数据(照片和所拍照片的元数据)...但是我有适当的代码来确保用户在使用 OnActivityResult 功能显示相机 activity 之前将数据输入到弹出窗口 activity 中(这样用户的照片具有的信息是作为元数据存储在我的 firebase 数据库中)。我想知道是否可以设置一个不等于 REQUESTCODE2 的请求代码,以便在按下后退按钮的情况下(这仍然会导致 REQUESTCODE2 被返回com.example.myapplication.nameofphoto activity,然后会触发 takepic()) 我可以故意确保请求代码是错误的,这样 takepic() 就不会触发,我也不会存储空数据进入我的数据库。

供您参考:nameofpersonvarnameofphotovar 都在不同的 class 中,并且是来自弹出窗口的信息 activity

private const val   REQUESTCODE = 2
private const val   REQUESTCODE2 = 3

fun take_pic(){
    val takephotoIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    if (takephotoIntent.resolveActivity(this.packageManager) != null) {
        startActivityForResult(takephotoIntent, REQUESTCODE)
    } else {
        Toast.makeText(this, "Unable To access Camera... ", Toast.LENGTH_LONG)
            .show()
    }


}
        photoButton.setOnClickListener {
            val action3 = Intent(this , com.example.myapplication.nameofphoto::class.java)
            startActivityForResult(action3, REQUESTCODE2 )

    }




override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (REQUESTCODE == requestCode && resultCode == Activity.RESULT_OK) {
            //Compressing the bitmap(image) into a byte[] to match the input of the .putbytes method
            val userimage = data?.extras?.get("data") as Bitmap
            val byteoutput = ByteArrayOutputStream()
            userimage.compress(Bitmap.CompressFormat.JPEG,100 , byteoutput)
            val data = byteoutput.toByteArray()
            //ref to the firebase "bucket" database
            val storageinfo = FirebaseStorage.getInstance().getReference("/Images" )
            //extra data that shows who the images belong to (users)

            val metadatastoreage = storageMetadata {
                setCustomMetadata("Name of person" , nameofpersonvar)
                setCustomMetadata("Name of photo" , nameofphotovar)}
           storageinfo.putBytes(data, metadatastoreage)



        }else if (requestCode ==REQUESTCODE2) {
            take_pic()

        }
        else {
            super.onActivityResult(requestCode, resultCode, data)
        }
        }

那你为什么不发送一些不同于当前activity打开的后按方法的结果代码并检查结果是否成功然后选择否则做一些事情。

将此作为后按方法的结果代码发送。 RESULT_CANCELED

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (REQUESTCODE == requestCode && resultCode == Activity.RESULT_OK) {
        //Compressing the bitmap(image) into a byte[] to match the input of the .putbytes method
        val userimage = data?.extras?.get("data") as Bitmap
        val byteoutput = ByteArrayOutputStream()
        userimage.compress(Bitmap.CompressFormat.JPEG,100 , byteoutput)
        val data = byteoutput.toByteArray()
        //ref to the firebase "bucket" database
        val storageinfo = FirebaseStorage.getInstance().getReference("/Images" )
        //extra data that shows who the images belong to (users)

        val metadatastoreage = storageMetadata {
            setCustomMetadata("Name of person" , nameofpersonvar)
            setCustomMetadata("Name of photo" , nameofphotovar)}
       storageinfo.putBytes(data, metadatastoreage)
       return
    }

   if (requestCode ==REQUESTCODE2 && resultcode == Activity.RESULT_OK) {
        take_pic()
   } else {
        //back pressed do something.
        //finish etc
    }
}

编辑:您可以覆盖弹出窗口中的 onBackPressed() activity 并使用 intent 将一些数据发送到父级 activity。例如

Intent resultIntent = new Intent();
// TODO Add extras or a data URI to this intent as appropriate.
resultIntent.putExtra("user_pic_click", "some data");
setResult(Activity.RESULT_OK, resultIntent);
finish();