user3025186
user3025186

Reputation: 43

send data from more than one activity to the one activity?

I want to send data to one activity from two activities... such as A,B and C when Activity A send data to Activity B also Activity C send data to Activity B how the Activity B know this data coming from A or C for example:

//this Activity A

Intent i=new Intent(this,B.class);

//send data by i such as id

//this Activity C

Intent in=new Intent(this,B.class);

//send data by in Intent such as id

//send data by in Intent such as email

//in activity B

Bundle val =getIntent().getExtras();

        if(val!=null)
            int id=val.getInt("id");
            String s=val.getString("email");
// to show

        Toast.makText(Main.this, id, Toast.LENGTH_LONG).show();

        Toast.makeText(Main.this, s, Toast.LENGTH_LONG).show();

Upvotes: 4

Views: 102

Answers (3)

Pankaj Arora
Pankaj Arora

Reputation: 10274

you can use a global class and save the values using getter setter.and on activity get the values using objects.

Upvotes: 0

amrinder007
amrinder007

Reputation: 1475

I also had the same problem then i solved it by adding FLAGS like from which activity the data is coming. Example:-

//this Activity A
Intent i=new Intent(this,B.class);
i.putExtra(key, value);
i.putExtra("from_which", "from_a");

//this Activity C
Intent i=new Intent(this,B.class);
i.putExtra(key, value);
i.putExtra("from_which", "from_c");

// in activity B
Intent intent = getIntent()
String from_which = intent.getStringExtra("from_which");

if(from_which.equals("from_a")){
    // from A
}
else if(from_which.equals("from_c")){
    // from C
}

Upvotes: 2

Coderji
Coderji

Reputation: 7745

how about you add a new extra that will make your get the previous activity like:

i.putExtra("Activity","A");

same goes for activity C:

i.putExtra("Activity, "C");

and in Activity B:

Bundle val=getIntent().getExtras();

if(val!=null)
    int id=val.getInt(Key);
    String s=val.getString("email");
    String activity = val.getString("Activity");

then use Activity variable to identify the precious Acitvity

hope this helps you out. check this question asked before

Upvotes: 2

Related Questions