Hello Old sports !
To the point, I have 3 activity as follows :
-Activity A
-Activity B
-Activity C
in activity A I create an intent to go to activity C:
Intent intent=new Intent(getActivity(),C.class);
startActivity(intent);
in activity B I create intent to go to activity C too:
Intent intent=new Intent(getActivity(),C.class);
startActivity(intent);
in activity C I am going to do something different if it's from A and B.
The question is what is the best practice on 'how to let activity C know whether activity A or B that is calling ?
-sorry lack English, greeting from bali..
You can pass a parameter with intent to another activity, in that way you can know which activity started.
Intent intent=new Intent(getActivity(),C.class);
intent.putString("activity","A"); // and same goes for B
startActivity(intent);
and in Activity C,
Intent intent = getIntent();
String previousActivity= intent.getStringExtra("activity");
What you can do here is, you can pass one value say flag = "A"
when it is from Activity A
and flag = B
when it is from Activity B
via Intent
and get that value in Activity C
...
Intent intent = new Intent(this, C.class);
intent.putExtra("flag", "A");
startActivity(intent);
Intent intent = new Intent(this, C.class);
intent.putExtra("flag", "B");
startActivity(intent);
Intent intent = getIntent();
String checkFlag= intent.getStringExtra("flag");
if(checkFlag.equals("A");
// It is from A
else
// It is from B
Also you can take static variable in your both activity and then pass the value of static variable with intent.
Like ex.
public static int a = 1;
and
public static int b = 2;
and then pass this with intent
and in your last activity get the value of static variable and done. You will be able to know that from which activity you came from.
You have two options.
1) Use startActivityForResult()
when calling activity C
. In this case you can use getCallingActivity()
method to find this out.
2) Add an extra to intent when calling activity C
.
// in activity A
Intent intent = new Intent(this, C.class);
intent.putExtra("calling", "A");
startActivity(intent);
// in activity B
String callingActivity = getIntent().getStringExtra("calling");
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