Android bundle object reference

Android Bundles

One way to save data when an Activity is destroyed is through the Bundle. This is just like the Intent you used earlier, in that it saves data in key/value pairs. Let’s set how the bundle works.

Have a look at the onCreate method again. The first line of the method is this:

protected void onCreate( Bundle savedInstanceState )

The Bundle part is a type, just like Int or String is a type. But this type handles keys and their values. The savedInstanceState part is just a variable name.

You can call various methods on your Bundle name. One of these methods is putInt. Another is putString. These methods are used to put data into a key/value pair. We want to save an integer value, so we could used this:

savedInstanceState.putInt( «HIGH_SCORE», someScore );

The key name we’ve come up with is HIGH_SCORE. The value for this key is whatever is inside of the variable called someScore.

Before our Activity is destroyed when the screen is rotated, Android allows you to override a method called onSaveInstanceState. This accepts a Bundle as a parameter.

Just below your displaySecondActivity method, add the following:

@override
public void onSaveInstanceState(Bundle mySavedInstanceState ) <

mySavedInstanceState.putInt( «HIGH_SCORE», someScore );

Your code should look like this:

We’ve given our Bundle the name mySavedInstanceState. This is then used to create the key/value pair. If Android destroys this Activity, then the method onSaveInstanceState is called. The first line is a call to the super class, which you must always do when Overriding a method. We then put our integer into the key called HIGH_SCORE.

We’re not quite done yet. We making sure to save data when the Activity is destroyed. But we’re not getting the data back anywhere. We can do that in the onCreate method.

When an app is first created, the savedInstanceState variable between the round brackets of onCreate will be null. It’s only not null when we start adding things to the Bundle object. We can test for this null state. Add this to you onCreate method, just before the final curly bracket:

if ( savedInstanceState != null ) <

someScore = savedInstanceState.getInt( HIGH_SCORE, 0 );

Your onCreate method should look like this:

To the left of an equal sign, we have our someScore variable. To the right, we’re calling a method on Bundle that has the name savedInstanceState. This method is getInt. This is used to get an integer value from the Bundle. If you had saved a string value then you’d use getString. In between the round brackets of getInt, we have the key name we set up, which was HIGH_SCORE. After a comma, you can type a default value, just in case something goes wrong. If it does, then the variable someScore will get a zero stored in it. If everything is OK, then getInt will grab the value associated with the HIGH_SCORE key.

Try it out. Run your app again. Click the button a few times and then rotate your device. Click the button again and you should find that the value in the Text View is incrementing properly, and not resetting to zero.

Exercise
You should also find that the Text View still says First Activity every time the device is rotated. You might want the number value displayed in the Text View at all times. Can you think of a way to fix this?

In the next lesson, we’ll take a look at the Android CardView.

Источник

Bundle Class

Definition

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

A mapping from String keys to various Parcelable values.

Remarks

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

Constructs a new, empty Bundle.

Constructs a new, empty Bundle.

Constructs a new, empty Bundle.

Constructs a new, empty Bundle.

Constructs a new, empty Bundle.

Properties

Returns the runtime class of this Object .

(Inherited from Object) ClassLoader

Return the ClassLoader currently associated with this Bundle.

An unmodifiable Bundle that is always #isEmpty() empty .

The handle to the underlying Android instance.

(Inherited from Object) HasFileDescriptors

Reports whether the bundle contains any parcelled file descriptors.

Returns true if the mapping of this Bundle is empty, false otherwise.

JniIdentityHashCode (Inherited from Object) JniPeerMembers PeerReference (Inherited from Object) ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Читайте также:  Пульт для андроида без рекламы

(Inherited from BaseBundle) ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

(Inherited from BaseBundle)

Methods

Removes all elements from the mapping of this Bundle.

Clones the current Bundle.

Returns true if the given key is contained in the mapping of this Bundle.

Make a deep copy of the given bundle.

Report the nature of this Parcelable’s contents

Dispose() (Inherited from Object) Dispose(Boolean) (Inherited from Object) Equals(Object)

Indicates whether some other object is «equal to» this one.

(Inherited from Object) Get(String)

Returns the entry with the given key as an object.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or false if no mapping of the desired type exists for the given key.

Returns the value associated with the given key, or defaultValue if no mapping of the desired type exists for the given key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or (byte) 0 if no mapping of the desired type exists for the given key.

Returns the value associated with the given key, or (byte) 0 if no mapping of the desired type exists for the given key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or (char) 0 if no mapping of the desired type exists for the given key.

Returns the value associated with the given key, or (char) 0 if no mapping of the desired type exists for the given key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or 0.0 if no mapping of the desired type exists for the given key.

Returns the value associated with the given key, or defaultValue if no mapping of the desired type exists for the given key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or 0.

Returns the value associated with the given key, or 0.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns a hash code value for the object.

Читайте также:  Андроид тв приставка tp link

(Inherited from Object) GetInt(String)

Returns the value associated with the given key, or 0 if no mapping of the desired type exists for the given key.

Returns the value associated with the given key, or defaultValue if no mapping of the desired type exists for the given key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or 0L if no mapping of the desired type exists for the given key.

Returns the value associated with the given key, or defaultValue if no mapping of the desired type exists for the given key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or (short) 0 if no mapping of the desired type exists for the given key.

Returns the value associated with the given key, or (short) 0 if no mapping of the desired type exists for the given key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or defaultValue if no mapping of the desired type exists for the given key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object) KeySet()

Returns a Set containing the Strings used as keys in this Bundle.

Wakes up a single thread that is waiting on this object’s monitor.

(Inherited from Object) NotifyAll()

Wakes up all threads that are waiting on this object’s monitor.

(Inherited from Object) PutAll(Bundle)

Inserts all mappings from the given Bundle into this Bundle.

Inserts all mappings from the given PersistableBundle into this BaseBundle.

(Inherited from BaseBundle) PutBinder(String, IBinder)

Inserts an IBinder value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a Boolean value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a boolean array value into the mapping of this Bundle, replacing any existing value for the given key.

Читайте также:  Exe program android что это

Inserts a Bundle value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a byte value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a byte array value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a char value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a char array value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a CharSequence value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a CharSequence value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a CharSequence array value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a CharSequence array value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts an ArrayList value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a double value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a double array value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a float value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a float array value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts an int value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts an int array value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts an ArrayList value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a long value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a long array value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a Parcelable value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts an array of Parcelable values into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a List of Parcelable values into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a Serializable value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a short value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a short array value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a Size value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a SizeF value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a SparceArray of Parcelable values into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a String value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts a String array value into the mapping of this Bundle, replacing any existing value for the given key.

Inserts an ArrayList value into the mapping of this Bundle, replacing any existing value for the given key.

Reads the Parcel contents into this Bundle, typically in order for it to be passed through an IBinder connection.

Removes any entry with the given key from the mapping of this Bundle.

Changes the ClassLoader this Bundle uses when instantiating objects.

Sets the Handle property.

(Inherited from Object) Size()

Returns the number of mappings contained in this Bundle.

Returns a string representation of the object.

(Inherited from Object) UnregisterFromRuntime() (Inherited from Object) Wait()

Causes the current thread to wait until another thread invokes the java.lang.Object#notify() method or the java.lang.Object#notifyAll() method for this object.

(Inherited from Object) Wait(Int64)

Causes the current thread to wait until another thread invokes the java.lang.Object#notify() method or the java.lang.Object#notifyAll() method for this object.

(Inherited from Object) Wait(Int64, Int32)

Causes the current thread to wait until another thread invokes the java.lang.Object#notify() method or the java.lang.Object#notifyAll() method for this object.

(Inherited from Object) WriteToParcel(Parcel, ParcelableWriteFlags)

Writes the Bundle contents to a Parcel, typically in order for it to be passed through an IBinder connection.

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

Performs an Android runtime-checked type conversion.

Источник

Оцените статью