I have 6 activities - A, B, C, D, E, F.
A opens B, B opens C and so on till F. I am currently at F and then from F, I opened B using Intent.FLAG_ACTIVITY_CLEAR_TOP and maintained A in backstack.
Now I want to send data back to A from B activity when back button is pressed. Please Help!!

- 463
- 2
- 15
-
Check this answer http://stackoverflow.com/a/10407371/1055241 – gprathour Dec 16 '16 at 12:16
-
@gprathour The answer provided in the link would be helpful if I was going from A->B and then needs data back to A on back press but here I am starting B activity from F and then needs data back to A. Plz read question one more time.. – Sahil Munjal Dec 16 '16 at 12:20
-
@SahilMunjal the link provided by gprathour is for you to get basic idea how startActivityForResult works you have to modify that according to your requirement. – Pavan Dec 16 '16 at 12:23
-
@Pavan Bro, I am already using startActivityForResult when I open activity from A->B and then I am able to send data back to A successfully. – Sahil Munjal Dec 16 '16 at 12:26
2 Answers
there will be two approach one if you use startActivityForResult then need to be used in all 6 activity this will require code change in all activity for onActivityResult this means you have to maintain all 6 activity in stack till your work complted when you complete with F just setresult with data and finish F which will received by E just pass received data with result to D and finish E you have to maintain this till A, this will lead to you extra coding and effort.
Second approach is don't maintain A in stack as you stated opened B using Intent.FLAG_ACTIVITY_CLEAR_TOP and maintained A in backstack. just like this open A with data you have in B, but i don't know this is feasible in your case.

- 5,016
- 1
- 26
- 30
You can use static Method to send data from one activity to another activity (when activities don't have direct link and also don't want to recreate first activity).
In First activty..
public class A extends Activity {
static A INSTANCE;
String data="";
@Override
public void onCreate(Bundle savedInstanceState) {
INSTANCE=this;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public static A getActivityInstance()
{
return INSTANCE;
}
public void getData(String b_data)
{
data = b_data;
}
}
Second Activity:
public class Second extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
}
}
@Override
public void onBackPressed()
{
A.getActivityInstance().getData("Your data");
super.onBackPressed();
}
Hope this will help you.

- 932
- 1
- 6
- 11