What is a “bundle” in an Android application

2019-01-01 14:22发布

What is a bundle in an Android application? When to use it?

12条回答
荒废的爱情
2楼-- · 2019-01-01 14:36

Bundle is not only to transfer data between two different components but more importantly it is used to restore the values stored before activity is destroyed into new activity.

such as the text in an EditText widget or the scroll position of a ListView.

查看更多
一个人的天荒地老
3楼-- · 2019-01-01 14:37

Bundles are generally used for passing data between various Android activities. It depends on you what type of values you want to pass, but bundles can hold all types of values and pass them to the new activity.

You can use it like this:

Intent intent = new...
Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("myKey", AnyValue);  
startActivity(intent);

You can get the passed values by doing:

Bundle extras = intent.getExtras(); 
String tmp = extras.getString("myKey");

You can find more info at:

查看更多
浪荡孟婆
4楼-- · 2019-01-01 14:37

Bundle:- A mapping from String values to various Parcelable types.

Bundle is generally used for passing data between various activities of android.

when we call onPause() then onStop() and then in reverse order onStop() to onPause().

The saved data that the system uses to restore the previous state is called the "instance state" and is a collection of key-value pairs stored in a Bundle object.

查看更多
回忆,回不去的记忆
5楼-- · 2019-01-01 14:40

Pass data between activities by using Bundle and Intent objects.


Your first create a Bundle object

Bundle b = new Bundle();

Then, associate the string data stored in anystring with bundle key "myname"

b.putString("myname", anystring);

Now, create an Intent object

Intent in = new Intent(getApplicationContext(), secondActivity.class);

Pass bundle object b to the intent

in.putExtras(b);

and start second activity

startActivity(in);

In the second activity, we have to access the data passed from the first activity

Intent in = getIntent();

Now, you need to get the data from the bundle

Bundle b = in.getExtras();

Finally, get the value of the string data associated with key named "myname"

String s = b.getString("myname");
查看更多
路过你的时光
6楼-- · 2019-01-01 14:40

Bundle is used to pass data between Activities. You can create a bundle, pass it to Intent that starts the activity which then can be used from the destination activity.

查看更多
看风景的人
7楼-- · 2019-01-01 14:40

bundle is used to share data between activities , and to save state of app in oncreate() method so that app will come to know where it was stopped ... I hope it helps :)

查看更多
登录 后发表回答