开发者

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...?


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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜