问题
What is a bundle in an Android application? When to use it?
回答1:
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:
android-using-bundle-for-sharing-variables and
Passing-Bundles-Around-Activities
回答2:
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");
回答3:
I have to add that bundles are used by activities to pass data to themselves in the future.
When the screen rotates, or when another activity is started, the method protected void onSaveInstanceState(Bundle outState)
is invoked, and the activity is destroyed. Later, another instance of the activity is created, and public void onCreate(Bundle savedInstanceState)
is called. When the first instance of activity is created, the bundle is null; and if the bundle is not null, the activity continues some business started by its predecessor.
Android automatically saves the text in text fields, but it does not save everything, and subtle bugs sometimes appear.
The most common anti-pattern, though, is assuming that onCreate()
does just initialization. It is wrong, because it also must restore the state.
There is an option to disable this "re-create activity on rotation" behavior, but it will not prevent restart-related bugs, it will just make them more difficult to mention.
Note also that the only method whose call is guaranteed when the activity is going to be destroyed is onPause()
. (See the activity life cycle graph in the docs.)
回答4:
A Bundle
is very much like a Java Map
object that maps String
keys to values. It's used to pass information between activities and other application components. It's also used by the framework to capture and restore state information.
The reason Android doesn't use plain old Map
objects for this is that Map
is too flexible; it can contain objects (such as, say, I/O streams) that cannot be serialized. The Bundle
API restricts the types of objects that can be added to a bundle in such a way that the bundle's contents are guaranteed to be serializable. The Android framework relies on this property.
I suggest that you read the documentation on Application Fundamentals. This explains, among other things, what bundles and intents are and what they are used for.
回答5:
Bundles can be used to send arbitrary data from one activity to another by way of Intents. When you broadcast an Intent, interested Activities (and other BroadcastRecievers) will be notified of this. An intent can contain a Bundle so that you can send extra data along with the Intent.
Bundles are key-value mappings, so in a way they are like a Hash, but they are not strictly limited to a single String / Foo object mapping. Note that only certain data types are considered "Parcelable" and they are explicitly spelled out in the Bundle API.
回答6:
Just create a bundle,
Bundle simple_bundle=new Bundle();
simple_bundle.putString("item1","value1");
Intent i=new Intent(getApplicationContext(),this_is_the_next_class.class);
i.putExtras(simple_bundle);
startActivity(i);
IN the "this_is_the_next_class.class"
You can retrieve the items like this.
Intent receive_i=getIntent();
Bundle my_bundle_received=receive_i.getExtras();
my_bundle_received.get("item1");
Log.d("Value","--"+my_bundle_received.get("item1").toString);
回答7:
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.
回答8:
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.
回答9:
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 :)
回答10:
use of bundle send data from one activity to another activity with the help of intent object; Bundle hold the data that can be any type.
Now I tell that how to create bundle passing data between two activity.
Step 1: On First activity
Bundle b=new Bundle();
b.putString("mkv",anystring);
Intent in=new Intent(getApplicationContext(),secondActivity.class);
in.putExtras(b);
startActivity(in);
Step 2: On Second Activity
Intent in=getIntent();
Bundle b=in.getExtras();
String s=b.getString("mkv");
I think this is useful for you...........
回答11:
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
.
回答12:
First activity:
String food = (String)((Spinner)findViewById(R.id.food)).getSelectedItem();
RadioButton rb = (RadioButton) findViewById(R.id.rb);
Intent i = new Intent(this,secondActivity.class);
i.putExtra("food",food);
i.putExtra("rb",rb.isChecked());
Second activity:
String food = getIntent().getExtras().getString("food");
Boolean rb = getIntent().getExtras().getBoolean("rb");
来源:https://stackoverflow.com/questions/4999991/what-is-a-bundle-in-an-android-application