Example usage for android.os Bundle putParcelableArray

List of usage examples for android.os Bundle putParcelableArray

Introduction

In this page you can find the example usage for android.os Bundle putParcelableArray.

Prototype

public void putParcelableArray(@Nullable String key, @Nullable Parcelable[] value) 

Source Link

Document

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

Usage

From source file:com.btmura.android.reddit.app.FragmentStateItemPagerAdapter.java

@Override
public Parcelable saveState() {
    Bundle state = null;
    if (mItemIds != null && mItemIds.length > 0) {
        state = new Bundle();
        state.putLongArray("itemIds", mItemIds);
    }/*from  w w w .j  a  v  a2  s. c o  m*/
    if (mSavedState.size() > 0) {
        if (state == null) {
            state = new Bundle();
        }
        Fragment.SavedState[] fss = new Fragment.SavedState[mSavedState.size()];
        mSavedState.toArray(fss);
        state.putParcelableArray("states", fss);
    }
    for (int i = 0; i < mFragments.size(); i++) {
        Fragment f = mFragments.get(i);
        if (f != null) {
            if (state == null) {
                state = new Bundle();
            }
            String key = "f" + i;
            mFragmentManager.putFragment(state, key, f);
        }
    }
    return state;
}

From source file:uk.co.threeonefour.android.snowball.activities.game.GameActivity.java

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {

    if (initialised) {
        savedInstanceState.putParcelable(GAME_STATE, (Parcelable) level9.getGameState());
        savedInstanceState.putParcelableArray(RAM_SAVE_SLOTS_STATE, ramSaveSlots);
        savedInstanceState.putParcelable(IMAGE_STATE, imageView.getBitmap());
        savedInstanceState.putInt(GAME_STATE_SLOT, gameStateSlot);
    }//from  w  ww. ja  v a 2 s .  c  o m

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

From source file:com.vk.sdk.dialogs.VKShareDialog.java

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString(SHARE_TEXT_KEY, mShareTextField.getText().toString());
    if (mAttachmentLink != null)
        outState.putParcelable(SHARE_LINK_KEY, mAttachmentLink);
    if (mAttachmentImages != null)
        outState.putParcelableArray(SHARE_IMAGES_KEY, mAttachmentImages);
    if (mExistingPhotos != null)
        outState.putParcelable(SHARE_UPLOADED_IMAGES_KEY, mExistingPhotos);
}

From source file:com.example.craiger.nav.FragmentItemIdStatePagerAdapter.java

@Override
public Parcelable saveState() {
    Bundle state = null;
    if (mSavedState.size() > 0) {
        state = new Bundle();
        long[] itemIdsForState = new long[mSavedState.size()];
        Fragment.SavedState[] fss = new Fragment.SavedState[mSavedState.size()];
        for (int i = 0; i < mSavedState.size(); i++) {
            itemIdsForState[i] = mSavedState.keyAt(i);
            fss[i] = mSavedState.valueAt(i);
        }//from www .  j a v a2s. c  om
        state.putLongArray("itemIdsForState", itemIdsForState);
        state.putParcelableArray("states", fss);
    }
    for (Map.Entry<Fragment, Long> fragmentToIdEntry : mFragmentToItemIdMap.entrySet()) {
        Fragment f = fragmentToIdEntry.getKey();
        if (f != null && f.isAdded()) {
            if (state == null) {
                state = new Bundle();
            }
            Long itemId = fragmentToIdEntry.getValue();
            String bundleKey = KEY_FRAGMENT + itemId;
            mFragmentManager.putFragment(state, bundleKey, f);
        }
    }
    return state;
}

From source file:com.kevinread.sortablefragmentpager.SortableFragmentStatePagerAdapter.java

@Override
public Parcelable saveState() {
    Bundle state = null;

    mItemIds = new long[getCount()];
    for (int i = 0; i < mItemIds.length; i++) {
        mItemIds[i] = getItemId(i);//from  w  ww.j  a v  a  2 s  .co  m
    }
    if (mSavedState.size() > 0) {
        state = new Bundle();

        if (mItemIds.length > 0) {
            state.putLongArray("itemids", mItemIds);
        }

        Fragment.SavedState[] fss = new Fragment.SavedState[mSavedState.size()];
        mSavedState.toArray(fss);
        state.putParcelableArray("states", fss);
    }
    for (int i = 0; i < mFragments.size(); i++) {
        Fragment f = mFragments.get(i);
        if (f != null) {
            if (state == null) {
                state = new Bundle();
            }
            String key = "f" + i;
            mFragmentManager.putFragment(state, key, f);
        }
    }
    return state;
}

From source file:com.bmd.android.collection.SimpleArrayMapTest.java

public void testParcelable() {

    final ParcelableObjectSparseObjectEntry[] parcelableArray = AndroidCollections.iterate(mArray).only()
            .values("4", "1").toParcelableArray(ParcelableObjectSparseObjectEntry.class);

    assertThat(parcelableArray).hasSize(2);
    assertThat(parcelableArray[0].getKey()).isEqualTo(1);
    assertThat(parcelableArray[0].getValue()).isEqualTo("1");
    assertThat(parcelableArray[1].getKey()).isEqualTo(4);
    assertThat(parcelableArray[1].getValue()).isEqualTo("4");

    final ArrayList<ParcelableObjectSparseObjectEntry<Integer, String>> parcelableList = AndroidCollections
            .iterate(mArray).but().keys(Arrays.asList(1, 2, 3)).reverse().toParcelableList();

    assertThat(parcelableList).hasSize(2);
    assertThat(parcelableList.get(0).getKey()).isEqualTo(4);
    assertThat(parcelableList.get(0).getValue()).isEqualTo("4");
    assertThat(parcelableList.get(1).getKey()).isEqualTo(0);
    assertThat(parcelableList.get(1).getValue()).isEqualTo("0");
    assertThat(parcelableList).containsExactly(SparseEntries.parcelableEntry(Integer.valueOf(4), "4"),
            SparseEntries.parcelableEntry(Integer.valueOf(0), "0"));

    final Bundle bundle = new Bundle();
    bundle.putParcelableArray("array", parcelableArray);
    bundle.putParcelableArrayList("list", parcelableList);

    final Parcel parcel = Parcel.obtain();
    bundle.writeToParcel(parcel, 0);/*  www  . j  ava  2 s .c  o m*/

    parcel.setDataPosition(0);

    final Bundle out = parcel.readBundle();
    out.setClassLoader(AndroidCollections.class.getClassLoader());

    assertThat(out.getParcelableArray("array")).isEqualTo(parcelableArray);
    assertThat(out.getParcelableArrayList("list")).isEqualTo(new ArrayList<Parcelable>(parcelableList));

    final ArrayList<ParcelableObjectSparseObjectEntry<Integer, String>> filledList = new ArrayList<ParcelableObjectSparseObjectEntry<Integer, String>>(
            2);

    AndroidCollections.iterate(mArray).but().keys(1, 2, 3).reverse().fillParcelable(filledList);

    assertThat(filledList).isEqualTo(parcelableList);

    final ParcelableObjectSparseObjectEntry[] filledArray = new ParcelableObjectSparseObjectEntry[2];

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(Integer.valueOf(2), "2"));

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray, 1);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(Integer.valueOf(2), "2"));
    assertThat(filledArray[0]).isEqualTo(filledArray[1]);

    try {

        AndroidCollections.iterate(mArray).fillParcelable(filledArray);

        fail();

    } catch (final IndexOutOfBoundsException e) {

    }

    final Parcelable[] parcelables = new Parcelable[2];

    AndroidCollections.iterate(mArray).only().to(1).fillParcelable(parcelables);

    assertThat(parcelables).containsExactly(filledArray);
}

From source file:org.lyricue.android.Lyricue.java

@Override
protected void onSaveInstanceState(Bundle outState) {
    for (String key : fragments.keySet()) {
        if (getSupportFragmentManager() != null) {
            if (fragments.get(key) != null) {
                getSupportFragmentManager().putFragment(outState, key, fragments.get(key));
            }//from w  w w.j  av a  2 s. c  o  m
        }
    }
    outState.putParcelableArray("hosts", hosts);
    outState.putString("profile", profile);
    outState.putLong("playlistid", playlistid);
    outState.putStringArray("playlists_text", playlists_text);
    outState.putLongArray("playlists_id", playlists_id);
    outState.putStringArray("bibles_text", bibles_text);
    outState.putStringArray("bibles_id", bibles_id);
    outState.putStringArray("bibles_type", bibles_type);
    super.onSaveInstanceState(outState);
}

From source file:org.opendatakit.common.android.utilities.AndroidUtils.java

public static Bundle convertToBundle(JSONObject valueMap, final MacroStringExpander expander)
        throws JSONException {
    Bundle b = new Bundle();
    @SuppressWarnings("unchecked")
    Iterator<String> cur = valueMap.keys();
    while (cur.hasNext()) {
        String key = cur.next();/* w w  w .j  a  v  a2  s . c  o m*/
        if (!valueMap.isNull(key)) {
            Object o = valueMap.get(key);
            if (o instanceof JSONObject) {
                Bundle be = convertToBundle((JSONObject) o, expander);
                b.putBundle(key, be);
            } else if (o instanceof JSONArray) {
                JSONArray a = (JSONArray) o;
                // only non-empty arrays are written into the Bundle
                // first non-null element defines data type
                // for the array
                Object oe = null;
                for (int j = 0; j < a.length(); ++j) {
                    if (!a.isNull(j)) {
                        oe = a.get(j);
                        break;
                    }
                }
                if (oe != null) {
                    if (oe instanceof JSONObject) {
                        Bundle[] va = new Bundle[a.length()];
                        for (int j = 0; j < a.length(); ++j) {
                            if (a.isNull(j)) {
                                va[j] = null;
                            } else {
                                va[j] = convertToBundle((JSONObject) a.getJSONObject(j), expander);
                            }
                        }
                        b.putParcelableArray(key, va);
                    } else if (oe instanceof JSONArray) {
                        throw new JSONException("Unable to convert nested arrays");
                    } else if (oe instanceof String) {
                        String[] va = new String[a.length()];
                        for (int j = 0; j < a.length(); ++j) {
                            if (a.isNull(j)) {
                                va[j] = null;
                            } else {
                                va[j] = a.getString(j);
                            }
                        }
                        b.putStringArray(key, va);
                    } else if (oe instanceof Boolean) {
                        boolean[] va = new boolean[a.length()];
                        for (int j = 0; j < a.length(); ++j) {
                            if (a.isNull(j)) {
                                va[j] = false;
                            } else {
                                va[j] = a.getBoolean(j);
                            }
                        }
                        b.putBooleanArray(key, va);
                    } else if (oe instanceof Integer) {
                        int[] va = new int[a.length()];
                        for (int j = 0; j < a.length(); ++j) {
                            if (a.isNull(j)) {
                                va[j] = 0;
                            } else {
                                va[j] = a.getInt(j);
                            }
                        }
                        b.putIntArray(key, va);
                    } else if (oe instanceof Long) {
                        long[] va = new long[a.length()];
                        for (int j = 0; j < a.length(); ++j) {
                            if (a.isNull(j)) {
                                va[j] = 0;
                            } else {
                                va[j] = a.getLong(j);
                            }
                        }
                        b.putLongArray(key, va);
                    } else if (oe instanceof Double) {
                        double[] va = new double[a.length()];
                        for (int j = 0; j < a.length(); ++j) {
                            if (a.isNull(j)) {
                                va[j] = Double.NaN;
                            } else {
                                va[j] = a.getDouble(j);
                            }
                        }
                        b.putDoubleArray(key, va);
                    }
                }
            } else if (o instanceof String) {
                String v = valueMap.getString(key);
                if (expander != null) {
                    v = expander.expandString(v);
                }
                b.putString(key, v);
            } else if (o instanceof Boolean) {
                b.putBoolean(key, valueMap.getBoolean(key));
            } else if (o instanceof Integer) {
                b.putInt(key, valueMap.getInt(key));
            } else if (o instanceof Long) {
                b.putLong(key, valueMap.getLong(key));
            } else if (o instanceof Double) {
                b.putDouble(key, valueMap.getDouble(key));
            }
        }
    }
    return b;
}

From source file:tv.ouya.sdk.TestOuyaFacade.java

/**
 * Save the products and receipts if we're going for a restart
 *//*from  w  ww .  java 2s . com*/

public void onSaveInstanceState(final Bundle outState) {
    if (mProductList != null) {
        outState.putParcelableArray(PRODUCTS_INSTANCE_STATE_KEY,
                mProductList.toArray(new Product[mProductList.size()]));
    }
    if (mReceiptList != null) {
        outState.putParcelableArray(RECEIPTS_INSTANCE_STATE_KEY,
                mReceiptList.toArray(new Receipt[mReceiptList.size()]));
    }
}

From source file:com.bmd.android.collection.SparseArrayCompatTest.java

public void testParcelable() {

    final ParcelableIntSparseObjectEntry[] parcelableArray = AndroidCollections.iterate(mArray).only()
            .values("4", "1").toParcelableArray(ParcelableIntSparseObjectEntry.class);

    assertThat(parcelableArray).hasSize(2);
    assertThat(parcelableArray[0].getKey()).isEqualTo(1);
    assertThat(parcelableArray[0].getValue()).isEqualTo("1");
    assertThat(parcelableArray[1].getKey()).isEqualTo(4);
    assertThat(parcelableArray[1].getValue()).isEqualTo("4");

    final ArrayList<ParcelableIntSparseObjectEntry<String>> parcelableList = AndroidCollections.iterate(mArray)
            .but().keys(Arrays.asList(1, 2, 3)).reverse().toParcelableList();

    assertThat(parcelableList).hasSize(2);
    assertThat(parcelableList.get(0).getKey()).isEqualTo(4);
    assertThat(parcelableList.get(0).getValue()).isEqualTo("4");
    assertThat(parcelableList.get(1).getKey()).isEqualTo(0);
    assertThat(parcelableList.get(1).getValue()).isEqualTo("0");
    assertThat(parcelableList).containsExactly(SparseEntries.parcelableEntry(4, "4"),
            SparseEntries.parcelableEntry(0, "0"));

    final Bundle bundle = new Bundle();
    bundle.putParcelableArray("array", parcelableArray);
    bundle.putParcelableArrayList("list", parcelableList);

    final Parcel parcel = Parcel.obtain();
    bundle.writeToParcel(parcel, 0);/*ww  w . j  a  v  a  2s .  co m*/

    parcel.setDataPosition(0);

    final Bundle out = parcel.readBundle();
    out.setClassLoader(AndroidCollections.class.getClassLoader());

    assertThat(out.getParcelableArray("array")).isEqualTo(parcelableArray);
    assertThat(out.getParcelableArrayList("list")).isEqualTo(new ArrayList<Parcelable>(parcelableList));

    final ArrayList<ParcelableIntSparseObjectEntry<String>> filledList = new ArrayList<ParcelableIntSparseObjectEntry<String>>(
            2);

    AndroidCollections.iterate(mArray).but().keys(1, 2, 3).reverse().fillParcelable(filledList);

    assertThat(filledList).isEqualTo(parcelableList);

    final ParcelableIntSparseObjectEntry[] filledArray = new ParcelableIntSparseObjectEntry[2];

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2, "2"));

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray, 1);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2, "2"));
    assertThat(filledArray[0]).isEqualTo(filledArray[1]);

    try {

        AndroidCollections.iterate(mArray).fillParcelable(filledArray);

        fail();

    } catch (final IndexOutOfBoundsException e) {

    }

    final Parcelable[] parcelables = new Parcelable[2];

    AndroidCollections.iterate(mArray).only().to(1).fillParcelable(parcelables);

    assertThat(parcelables).containsExactly(filledArray);
}