Consider I've 3 activities Activity1 Activity2 Activity3 if I want to send back data to Activity1 from Activity3 skipping Activity2 what should I do?
There is a "correct" way to this adding FLAG_ACTIVITY_FORWARD_RESULT to the intent.
Which is used to start the next activity and notify it to pass the result to the first one:
Code:
Activity 1 -> startActivityForResult(activityB,0);
Activity 2 -> activityCintent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(activityCintent); finish();
Activity 3 -> setresult(10); finish();
Activity 1 -> if(result==10) {dofunction(); }
Restarting of activity1 again is not a good way.
You can achieve this using "startActivityForResult"
Use below code, to start Activity2
Intent intent = new Intent(mContext, Activity2.class);
startActivityForResult(intent, REQUEST_CODE);
Use below code, to start Activity3
Intent intent = new Intent(mContext, Activity3.class);
startActivityForResult(intent, REQUEST_CODE);
In activity3 use the below code where you want to send the data to activity1:
Intent intent = getIntent();
intent.putExtra("Key", value);
setResult(RESULT_OK, intent);
finish();
Override onActivityResult in Activity1 and Activity2.
In onActivityResult of activity2,check the request code and Result code. Based on this call finish(), and set the values as below :
boolean value = data.getBooleanExtra("key, false);
Intent intent = getIntent();
intent.putExtra("Key", Value);
setResult(RESULT_OK, intent);
finish();
In onActivityResult of activity1 you will get the data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With