startActivityForResult() is not pausing previous Activity

586 Views Asked by At

I have Activities A, B, and C. A starts B and C, one after the other. The problem is that B is opened, then C starts and covers B. I want A to wait until B has finished and returned, but that is not happening. C starts immediately after starting B, so the user never sees B.

I am using startActivityForResult() as suggested in other posts, but that is not helping.

Here is the code in Activity A:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (! pm.isIgnoringBatteryOptimizations(getApplicationContext().getPackageName())) {
    startActivityForResult(new Intent(android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS), 1);
}

...

startActivity(new Intent(InitActivity.this, MyAwesomeActivity.class));
2

There are 2 best solutions below

0
On BEST ANSWER

You have to chain the activities using onActivityResult().

A calls B, then in onActivityResult() you use the resultCode parameter to verify that B is returning. Once verified that you are in the callback from B, then you call C. So it's more like A starts B, then B starts C.

0
On

You have to use startActivityForResult. You have to basically do the following three steps:

  1. startActivityForResult(intent_A_to_B, 101);//here 101 is any request code
  2. wherever you call finish() inside Activity B, call setResult(RESULT_OK)
  3. In Activity A, override onActivityResult.

    @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 101) {//this is the request code if (resultCode == RESULT_OK) { startActivity(intent_A_to_C); } }