0

I have 3 Activities:

  • A: Parent Activity
  • B: first Child Activity
  • C: second Child Activity

In A Activity open B Activity when button is clicked

Intent i = new Intent(this,  ActivityB.class);
startActivityForResult(i, 1); 

In A Activity there is a Runnable object that in some condition open C Activity

Intent i = new Intent(this,  Activityc.class);
startActivityForResult(i, 2); 

All work and on device i see C Activity over B Activity over A Activity but when i close C Activity with this.finish(); in A Activity onActivityResult() event is not fired. It'll be fired only if i close the B Activity too.

How can i know in A Activity when C Activity is closed and let B Activity opened?

MERCCK
  • 63
  • 8
  • intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); use on B Activity – Naveen Tamrakar May 18 '16 at 13:38
  • When B is in foreground then A will be in background. Why would you want it to know if C has closed? Also, SO could kill your A while B is in foreground because of reasons (see "Do not keep activities"). – Pedro Oliveira May 18 '16 at 13:41
  • B Activity must stay opened and A activity must do some async task when C is closed. – MERCCK May 18 '16 at 13:44
  • You can't guarantee that that is going to work. I suggest you use another approach like a singleton that performs that task for example. Because there are several situations where the system can destroy your Activity A if it's not on foreground. – Pedro Oliveira May 18 '16 at 13:46
  • @NaveenTamrakar i have added FLAG_ACTIVITY_CLEAR_TOP to ActivityB intent but nothing is changed – MERCCK May 18 '16 at 13:49
  • @PedroOliveira if i close A Activity how can i retrive result in `onActivityResult()` event ? – MERCCK May 18 '16 at 13:52
  • A will always receive the result but only after all activities above it are finished – Pedro Oliveira May 18 '16 at 14:03
  • @PedroOliveira that is my problem... I need C result while B remains opened – MERCCK May 18 '16 at 14:10
  • Then you will need to change your app architecture. You will need a manager (a singleton class) that manages all that results. When C terminates it notifies the singleton to start the task with the results on it. ActivityA when onResuming will call that singleton and updates it's ui with the data from the task (that may or may be not finished already). – Pedro Oliveira May 18 '16 at 14:12

1 Answers1

0

Whenever you wish to exit all open activities, you should press a button which loads the first Activity that runs when your application starts then clear all the other activities, then have the last remaining activity finish. to do so apply the following code in ur project

Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

The above code finishes all the activities except for FirstActivity. Then we need to finish the FirstActivity's Enter the below code in Firstactivity's oncreate

if (getIntent().getBooleanExtra("EXIT", false)) {
    finish();
}

Please See this

Community
  • 1
  • 1
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
  • i don't need to close B Activity, but i need to know when C is closed and with which result in A Activity – MERCCK May 18 '16 at 13:56