I have an activity which is called by few other activities. For example: I have Activity1,Activity2,Activity3. Activity1 calls Activity2 and pass parameter. Activity3 also calls Activity2 and pass parameter.
Now based on the calling activity, Activity2 performs some task. But how do I know which activity is calling Activity2?? can anybody plz help me??
I successfully use: (Activity).getLocalClassName()
If you start the activity with
startActivityForResult(Intent, int)
, then you can get calling activity bygetCallingActivity().getClassName()
.You could pass an additional parameter that specifies the calling
Activity
.Pass anything(String/ int etc.) to putExtra and base on that do the your work like
And then receive in child like
then
I'm Using This line
i hope it work with you
A. If you can use startActivityForResult
As per Zain Ali's answer below: If you can start Activity with startActivityForResult() then you can get name of calling Activity class by this.getCallingActivity().getClassName();
B. If you can not use startActivityForResult
If you can not use startActivityForResult(), then you can use following method: You can pass additional parameter in intent, check the value in activity and act accordingly.
1) Define an interface or constants class to define integer constants to indicate calling activity
2) Add extra parameter in intent while calling Activity2.
3) Check the value of this extra parameter in Activity2 and act accordingly..