0

I have three activities : A , B, C. My application flow is : A -> B & B->A or A -> B & B ->C & C->A. So i used startActivityForResult to pass data from A to another activities and in A i also have onActivityResult to handle received data. In B, I change data and go to C by:

Intent intent = new Intent(this,C.class);
Bundle bundle = this.getIntent().getExtras();
bundle.putSerializable("newdatafromA", newdatafromA);
intent.putExtras(bundle);
startActivity(intent);

In C, I get data and change something. I try to setResult() with result code and go to A but it not success:

Intent positveActivity = new Intent(getApplicationContext(),A.class);
Bundle bundle = new Bundle();
bundle.putSerializable("newdata", newdata);
bundle.putSerializable("newdatafromA", newdatafromA);
positveActivity.putExtra("data", bundle);
setResult(2, positveActivity);
startActivity(positveActivity);

I debug and it dont jump to onActivityResult(I handle result code =2 in here) in A.class. and bundle have all data. Any idea to help me resolve it ?

ducanhZ5Z
  • 73
  • 2
  • 10

3 Answers3

3

From your Activity B. When you are going to Activity C. then use startActivityForResult then in Your Activity B. override onActivityResult and handle the Data came from Activity C. then pass the data back to Activity A by setResult just you did in activity C. So this Data will be passed back to Activity A

Flow:

Activity A --> Activity B --> Activity C then back from Activity C --> Activity B and finally back to Activity A

Abdul Mohsin
  • 1,037
  • 1
  • 7
  • 20
  • 1
    You are so so awesome. This working great !!! tks U so much. I spend too much time to search for it and the answer is so simple :( – ducanhZ5Z Nov 20 '14 at 07:59
  • Sorry bro. I dont have enough reputation to vote. But your answers is the most correct. tks again. – ducanhZ5Z Nov 20 '14 at 10:29
  • 1
    @AbdulMohsin I voted up on behalf of ducanhZ5Z since your answer had helped me a while back in think through the logic. – RmK Jun 26 '15 at 00:27
0

to get back to OnactivityResult you should not create an intent and then startActivity A..

You just have to call finish(); method and will automatically return to Activity A and run onActivityResult ...

Like : in Activity C:

Bundle bundle = new Bundle();
bundle.putSerializable("newdata", newdata);
bundle.putSerializable("newdatafromA", newdatafromA);
positveActivity.putExtra("data", bundle);
setResult(2, positveActivity);
finish();

Hope it helps :)

Makwana
  • 70
  • 9
0

When you start Activity A from activity C, start using normal method only but -

Use Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP as your flag. Then it will work the way you desire.

 Intent intent = new Intent(C.this,A.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);

Source here

Community
  • 1
  • 1
Darpan
  • 5,193
  • 3
  • 45
  • 74