1

I have ActivityA from where I open another activity:

val intent = Intent(this, ActivityC::class.java)
startActivityForResult(intent,  99)

When I finish ActivityC as expected, I get the callback in onActivityResult of ActivityA.

The problem is that if I open another activity from ActivityA like:

startActivity(Intent(this, ActivityB::class.java))

before calling finish() in ActivityC, and then do finish() in ActivityC I don't get the callback.

Is there any workaround about this?

CODE

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<TextView>(R.id.clickme).setOnClickListener {
            startActivityForResult(Intent(this, MainActivity2::class.java), 99)

            Handler().postDelayed({
                startActivity(Intent(this, MainActivity3::class.java))
            }, 1000)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        System.out.println("Came to onActivityResult")
    }
}

Activity2:

class MainActivity2 : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        Handler().postDelayed({
            setResult(RESULT_OK)
            finish()
        }, 2000)
    }
}

Removing the call:

startActivity(Intent(this, MainActivity3::class.java))

Will trigger the onActivityResult as expected.

user677767
  • 203
  • 3
  • 10
  • please share your code, there might be some mistake in code... – Hafiza Jun 15 '21 at 10:48
  • @hafiza I think it's not a code mistake. You can try with the example above. – user677767 Jun 15 '21 at 11:13
  • 1
    I don't think you're intended to do this. An Activity shouldn't be starting multiple other Activities simultaneously. It's going to open you up to all kinds of possible bugs with the order of the task stack. Open the second activity and pass it an extra that can tell it if it should open another activity after itself. That way, each Activity has a clear parent and child, and the task stack is kept orderly. – Tenfour04 Jun 15 '21 at 12:55
  • This seems to be answered here https://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android – ezaspi Jun 15 '21 at 13:11

0 Answers0