Reputation: 54949
I have 3 activities. A, B and C. From A to C and B to C. How can i from Activity C find out which activity was loaded previously and refereed to this Activity.
Upvotes: 0
Views: 355
Reputation: 14746
If you start your activity C with startActivityForResult instead of startActivity, you have access to the calling Activity:
Start Activity C like this:
Intent intent = new Intent(this, C.class);
int requestCode = 1; // Or some other integer
startActivityForResult(intent, requestCode);
in Activity C:
onCreate(...) {
String callingClassName = getCallingActivity().getClass().getSimpleName();
}
Upvotes: 1
Reputation: 32900
You could handle this through intent bundles. Basically in Activity A or B you launch Activity C as follows:
Intent launchIntent = new Intent(this, ActivityC.class);
launchIntent.putExtra("originActivity", this.getClass().getName());
In Activity C, you retrieve it like
public class ActivityC extends Activity{
onCreate(...){
Intent callingIntent = getIntent();
String originActivity = callingIntent.getStringExtra("originActivity");
}
}
Now I passed the activity name as string, you may include it in some more convenient way, using constants or something like that. You can look it up here.
Upvotes: 3