I need the flow of Activty4 Pass the data to Activity1 and close all the activities except Activity1.
Activity1-> open Activity2(it has tabview)
Activity2-> open Activity3
Activity3-> Pass the data to Activity1 and also close Activity2 and Activity3
I need the flow of Activty4 Pass the data to Activity1 and close all the activities except Activity1.
Activity1-> open Activity2(it has tabview)
Activity2-> open Activity3
Activity3-> Pass the data to Activity1 and also close Activity2 and Activity3
On activity1 use the startActivityForResult call to start the activity2 so you can get the result of it:
Intent i = new Intent(this, Activity2.class);
startActivityForResult(i, 1);
On activity2:
Intent i = new Intent(this, Activity3.class);
startActivityForResult(i, 2);
On activity3 at the point you set the result:
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
This sets the result and closes Activity3 with the call to finish.
Now, on Activity2 you should add this code to get the result:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
if (resultCode == Activity.RESULT_OK){
String result = data.getStringExtra("result");
Intent returnIntent = new Intent();
returnIntent.putExtra("result", result); // send the result of Activity3
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
And you can get the result on Activity1:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result = data.getStringExtra("result"); // Now you have the result here
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
Notice you should get consistent with the integers you set on the startActivityForResult and the requestCode you receive, I recommend using constants here.
If you need more info have a look at https://developer.android.com/training/basics/intents/result and How to manage startActivityForResult on Android?
IMHO the best choice is using Observable pattern.
Activity 4 has an observable object, Activity 1,2,3 will be registered to observe it.
When the data in Activity 4 is ready, Activity 4 will send a signal to notify that data has been ready then Activity 1 will receive the data and activity 2 & 3 will kill itself
Easiest way is following Whenever you wish to exit all open activities over another activity you can do it in this way
Do this in 3rd activity
Intent intent = new Intent(this, FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("your_params","param"); // data
startActivity(intent);
And in First Activity, in oncreate handle intent
getIntent().getStringExtra("your_params");
If OnCreate don't get called, please override onNewIntent method
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
getIntent().getStringExtra("your_params");
}