I have two activities a parent and a child activity but the child activity is also triggered by another activity. Now I want to return result from this child activity to the Parent activity ONLY when the child activity is triggered from this parent activity.
Is there any way the child can find out the caller activity (so that it does some calculations only for that caller activity). Is there any way I can avoid this:
intent.putExtra("caller", "MainActivity");
startActivity(intent);
I want this:
if (ParentActivity)
{ //do something and finish() }
if (AnotherActivity)
{ //do something more and finish() }
Yes you can do that by using startActivityForResult() from the Parent activity that you chose to be the one handling the results.
Basically in the Parent activity you can do:
Then you have to define a method to handle the results which is onActivityResult()
Inside the child activity, you can set the result by using setResult(). There are two variants of this method one of them gives you the possibility to pass in some data.
Please, read the documentation to find more about how to use it.
EDIT:
You can use: getCallingActivity() from the called activity to find which is the parent activity but that only works if you used startActivityForResult.