shyam
shyam

Reputation: 1306

send information through intent.putExtra()

i found several thread "title like me "but my qus is diffrent .my qus is using intent can i send the data to only next calling activity?can i dont use that getExtra() method in other activity means can i get the data in any activity via using "key"?

as for example...

     int number=5;   
     Intent i = new Intent(A.this, B.class);
     i.putExtra("numbers", number);
     startActivity(i);


     In the activity B i get the info:


     Bundle extras = getIntent().getExtras();
     int arrayB = extras.getInt("numbers");

can i get it in any other activity also...?

Upvotes: 1

Views: 1190

Answers (4)

Ovidiu Latcu
Ovidiu Latcu

Reputation: 72331

You will be able to achieve this using sendBroadcast() from the Activity that wants to send data to multiple Activities and by registering BroadcastReceivers inside the Activities that you want to receive the Intent. You can have a look at this tutorial Basic of Android: Intent Receivers to get a start on BroadcastReceivers.

Upvotes: 1

user370305
user370305

Reputation: 109237

as your code suggests,

 int number=5;   
 Intent i = new Intent(A.this, B.class);
 i.putExtra("numbers", number);
 startActivity(i);

and from the line

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

your bundle object get the resources in only B activity.

EDIT: you get the bundle object in only that activity for which you had pass the intent. so, only that activity receive it.

And if you want to access data in other activity make use of shared preference, sqlite, internal storage file etc..

Thanx.

Upvotes: 1

Pratik
Pratik

Reputation: 30855

no. you cannot get this in another activity unless you can not send this intent to that Activity.

An Intent object is a bundle of information. when you startActivity we pass the information to that activity. If you want to use this bundle into another activity you have pass to that activity.

you can do to pass the bundle of information as the level like

A->B->C

here Activity A start the activity and pass the information to the B Activity and same bundle you can pass to the C Activity as you send directly A->C

Upvotes: 1

Ian Low
Ian Low

Reputation: 399

your intent will only reference one activity (in this case, B.activity.class) so only B will be able to pull up the extras.

if a third activity C requires the info, you will need to structure your program such that B will call another intent (C.class) passing the same extras down.

Upvotes: 1

Related Questions