0

I have three activities (e.g Activity A, Activity B, Activity C) and Activity A calls Activity B by calling startActivityForResult() method and then, Activity B calls Activity C.

Activity A should receive a return value from Activity C at onActivityResult() method finishing Activities C and B.

For expand question, it may need more activity steps (A -> B -> C -> D -> -> ... -> return to A) to get a result and the concept of question is the first activity should receive a return value from last activity at onActivityResult() method.

Process

  1. Activity A calls -> Activity B that calls -> Activity C
  2. Finish activities (B, C) and remain only the Activity A.
  3. A activity receive a result of C activity at onActivityResult method.

How can I do this?

Apply Scenario

  • Activity A : is main activity and has a start quiz button and score edit text.
  • When click the start button, start the activity B.
  • Activity B : shows round 1 quiz and when finish this, continue to next round.
  • Activity C : shows round 2 quiz and when finish this, continue to next round.
  • Activity D : shows round 3 quiz and when finish this, close B, C, D activity and return total score (round1 + round2 + round3) to A activity.

Activity A should be able to recognize the Activity D finished event, so show a grand total score when finished D.

Will Ray
  • 10,621
  • 3
  • 46
  • 61
S.J. Lim
  • 3,095
  • 3
  • 37
  • 55
  • In my opinion we have some points: 1. in Activity A you can not receive result from Activity C by "onActivityResult", just receive it from Activity B 2. If you want A receive result from C with out B, finish middle activities and then use callback(handle, event...) to send result(data) from last activity to the first. 3. Others wise, if you still want to use "onActivityResult" you should use as A call--> B call--> C return-->B return--> A – ThaiPD Dec 30 '15 at 02:32
  • If you can use fragments, that might make passing around values easier since you only go through one activity to the other fragment. – OneCricketeer Dec 30 '15 at 02:33
  • cricket_007/ I should solve this problem by using only the activity. :C – S.J. Lim Dec 30 '15 at 02:35
  • Possible duplicate of [How do I pass data between activities on Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android) – p4sh4 Dec 30 '15 at 02:35
  • Thanks Phan Dinh. I will use your advice. – S.J. Lim Dec 30 '15 at 04:48
  • You can store score in global(in application class) variable or SharedPreference and then use it. – Ketan Ahir Dec 30 '15 at 04:48
  • @KetanAhir The way to handle a finished activity D event is "onResume" method? I'd like to show a score when finished activity D. – S.J. Lim Dec 30 '15 at 04:56
  • @S.J.Lim yes you can consider that if you are in Activity A it mean B,C,D are finished. – Ketan Ahir Dec 30 '15 at 06:03

3 Answers3

2

You can call Activity B from A with startActivityForResults. Then do the same for Activity B to start Activity C. Override onActivityResult in Activity B and send that intent to Activity A in this method. Eg:

 @Override
 public void onActivityResult(int req, int res, Intent result)
 {
 super.onActivityResult(req, res, result);
 setResult(RESULT_CODE,result);
  }

Or you can create your own interface in class C and implement it on class A.

S Praveen Kumar
  • 341
  • 1
  • 13
0

Classic scenario to use an event bus in android you can either use an event bus library like otto or greenrobot or Rxjava, OR go with the classical approach of passing data through intents.

Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52
0

Try calling first activity from last activity using intent as below

Intent intent = new Intent(LastActivity.this, MainActivity.class);
 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 intent.putExtra("RESULT", "result to main activity");
 startActivity(intent);