What is a bundle
in an Android application? When to use it?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
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
Step 2: On Second Activity
I think this is useful for you...........
First activity:
Second activity:
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, andpublic 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.)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.
Just create a bundle,
IN the "this_is_the_next_class.class"
You can retrieve the items like this.
A
Bundle
is very much like a JavaMap
object that mapsString
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 thatMap
is too flexible; it can contain objects (such as, say, I/O streams) that cannot be serialized. TheBundle
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.