Reputation: 752
I have 3 Activitys
As you see those Activities 1 and 2 send different Items to Activity 3
i want to know is it possible to do this or not if it is possible please show me the way...
Activity1:
Intent in = new Intent(getApplicationContext(),Activity3.class);
in.putExtra("OrderID", tvOrderID.getText().toString());
in.putExtra("OrderSHOPNAME", tvShopName.getText().toString());
Activity2:
Intent in = new Intent(getApplicationContext(),Activity3.class);
in.putExtra("OrderQTY", tvOrderID.getText().toString());
in.putExtra("OrderCOLORNAME", tvShopName.getText().toString());
Activity3: ????? what should i put here for getting extra ????
as you can see there is four different putextra
is it possible to mentioned which activity is sending putextra ?
Upvotes: 0
Views: 474
Reputation: 12933
There is no limitation for the amount of elements you can put to the Intent extras. Also, it doesn't matter if different Activities add different amount of elements, if the Intent remains the same by reusing it. This is because the Intent extras is using a Bundle and this is just a HashMap.
I assume that there are 2 different Intents. Activity1 or Activity2 starts Activity3. This way you should add some indicator to the Intent to check which Activity has called Activity3. Based on this read the data to avoid NPEs and unnecessary data assignment.
// in Activity1 or 2
Intent i = new Intent(this, Activity3.class);
i.putExtra("Activity", 1);
i.putExtra("foo", "bar"); // repeat this line as you like
startActivity(i)
// in Activity3
Intent i = getIntent();
String s;
switch(i.getIntExtra("Activity")) {
case 1:
// get the data of Activity1
s = i.getStringExtra("foo");
break;
case 2:
// get the data of Activity2
break;
}
Upvotes: 1
Reputation: 4651
It's possible by extra
feature.
to send something to other activity you use .putExtra
ex:
to send something from activity 1
to activity 3
you do:
Intent i = new Intent(Activity1.this,Activity3.class);
//"" is name of extra thing (field)
//and after the comma is the item
i.putExtra("myname", "ahmed");
startActivity(i);
so now we sent a thing called myname
, which is ahmed
.
then in Activity3
we get the thing by getIntent();
Intent intent = getIntent();
then assigning the thing, like if it was a string like our case ( ahmed ) we do,:
//the between "" is the field name we retrieved from acitvity1
String name = intent.getStringExtra("myname");
so now the String name
value will be ahmed
.
Upvotes: 0