Search code examples
androidandroid-jetpack-composeregisterforactivityresult

Registerforactivityresult and rememberLauncherForActivityResult not waiting for Activity from Compose


The Activity is called successfully but is call after the result:ActivityResult is execute. Instead of waiting for the Activity to be exited/finished it does the Callback first then it opens the Activity.

On Debug I inspect the result and is getting a Result_Canceled, then execution continues opening the Activity. Then I do the activity and Click the exit button, but the result code is not executed.

Register code inside the MainActivity:

val startForResult: ActivityResultLauncher<Intent> =
    registerForActivityResult(
        ActivityResultContracts.StartActivityForResult(),
        ActivityResultCallback<ActivityResult> { result:ActivityResult ->
            if (result.getResultCode() == RESULT_OK) {
                // Here, no request code
                val result1 = result.data?.extras?.getString("pdfname")
                val result2 = result.data?.extras?.getByteArray("pdf")

                msgbox(this.applicationContext,"PDF file name: ${result1}", "Intent Result")
                pdfname.value = result1!!
                pdfBytes.value = result2
            }
        })

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

Calling Activity from Composable function:

Button(
        onClick = {
            val path = ""
            val intent = Intent(context, com.mastering.pdfactivity.MainActivity::class.java) 
            //// Module Library - Activity in Layout Style
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            if (filename != "") {
                intent.putExtra("pdfpath", path)
                intent.putExtra("pdfname", filename)
            }

            launcher.launch(intent)

        },
        modifier = Modifier
            .fillMaxWidth(),
        colors = ButtonDefaults.buttonColors(containerColor = Color(0XFF0F9D58))
    ) {
        Text(text = "Get Receipt from PDF", color = Color.White)
    }

PDFactivity Exit code (There is no other line using finish() nor setResult():

exitbtn.setOnClickListener {
        val result1: String?
        val result2: String?

        result1 = pdfpath
        result2 = pdfname
        val resultIntent = Intent()
        resultIntent.putExtra("pdfpath", result1)
        resultIntent.putExtra("pdfname", result2)
        setResult(RESULT_OK, resultIntent)
        finish()
    }

Feel free to ask for more code if needed to be able to help me.

I found a KBA for calling Activity from Compose but still does not work.

My new code is as follow:

val pdfPicker = rememberLauncherForActivityResult(
            contract = ActivityResultContracts.StartActivityForResult(),
            onResult = { result ->
                if (result.resultCode == RESULT_OK) {
                    // Here, no request code
                    val result1 = result.data?.extras?.getString("pdfname")
                    val result2 = result.data?.extras?.getByteArray("pdf")

                    msgbox(mContext,"PDF file name: $result1", "Intent Result")
                    pdfname.value = result1!!
                    pdfBytes.value = result2
                }
            }
        )

Changed Button code:

Button(
            onClick = {
                val path = ""
                val filename = ""
                val intent = Intent(mContext, com.mastering.pdfactivity.MainActivity::class.java)
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                if (filename != "") {
                    intent.putExtra("pdfpath", path)
                    intent.putExtra("pdfname", filename)
                }

                pdfPicker.launch(intent)

            },
            modifier = Modifier
                .fillMaxWidth(),
            colors = ButtonDefaults.buttonColors(containerColor = Color(0XFF0F9D58))
        ) {
            Text(text = "Get Receipt from PDF", color = Color.White)
        }

But it still runs out of order. The Activity runs after the result code is executed. Something is missing and I still do not see it.


Solution

  • I found the culprit of the problem:

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    

    The "FLAG_ACTIVITY_NEW_TASK" was making the Intent not to wait for the result.