Bundle类解析

Bundle类源码浅读

从String键到各种Parcelable值的映射。

使用场景:

  • activity中用于保存activity的状态信息
  • 使用Intent传递封装到Bundle中的数据

一、数据的存取方法:

1.简单类型的数据存入putXXX():

  • putByte(@Nullable String key, byte value)
  • putChar(@Nullable String key, char value)
  • putShort(@Nullable String key, short value)
  • putFloat(@Nullable String key, float value)
  • putCharSequence(@Nullable String key, @Nullable CharSequence value)
  • putParcelable(@Nullable String key, @Nullable Parcelable value)
  • putSerializable(@Nullable String key, @Nullable Serializable value)
向Bundle中放入一个可序列化的对象
  • putBundle(@Nullable String key, @Nullable Bundle value)
向Bundle中放入一个Bundle对象

2.Array数组的存入方法putXXXArray()

  • putByteArray(@Nullable String key, @Nullable byte[] value)
  • putShortArray(@Nullable String key, @Nullable short[] value)
  • putCharArray(@Nullable String key, @Nullable char[] value)
  • putFloatArray(@Nullable String key, @Nullable float[] value)
  • putCharSequenceArray(@Nullable String key, @Nullable CharSequence[] value)
  • putParcelableArray(@Nullable String key, @Nullable Parcelable[] value)
  • putSparseParcelableArray(@Nullable String key, @Nullable SparseArray

3.ArrayList存入的方法putXXXArrayList()

  • putIntegerArrayList(@Nullable String key, @Nullable ArrayList value)
  • putStringArrayList(@Nullable String key, @Nullable ArrayList value)
  • putCharSequenceArrayList(@Nullable String key, @Nullable ArrayList value)
  • putParcelableArrayList(@Nullable String key, @Nullable ArrayList

二、Bundle类中对bundle对象操作的方法

/**
 * Clones the current Bundle. The internal map is cloned, but the keys and
 * values to which it refers are copied by reference.
 */
@Override
public Object clone()

/**
 * Removes all elements from the mapping of this Bundle.
 */
@Override
public void clear()

/**
 * Removes any entry with the given key from the mapping of this Bundle.
 *
 * @param key a String key
 */
public void remove(String key)

/**
 * Inserts all mappings from the given Bundle into this Bundle.
 *
 * @param bundle a Bundle
 */
public void putAll(Bundle bundle)

猜你喜欢

转载自blog.csdn.net/sinat_34383316/article/details/75807874