Get an array of Bundle objects from a parcelable array field in a bundle. - Android Android OS

Android examples for Android OS:Parcel

Description

Get an array of Bundle objects from a parcelable array field in a bundle.

Demo Code


//package com.book2s;
import android.os.Bundle;
import android.os.Parcelable;
import java.util.Arrays;

public class Main {
    /**//from ww w . ja  va  2 s .c  om
     * Get an array of Bundle objects from a parcelable array field in a bundle.
     * Update the bundle to have a typed array so fetches in the future don't need
     * to do an array copy.
     */
    public static Bundle[] getBundleArrayFromBundle(Bundle bundle,
            String key) {
        Parcelable[] array = bundle.getParcelableArray(key);
        if (array instanceof Bundle[] || array == null) {
            return (Bundle[]) array;
        }
        Bundle[] typedArray = Arrays.copyOf(array, array.length,
                Bundle[].class);
        bundle.putParcelableArray(key, typedArray);
        return typedArray;
    }
}

Related Tutorials