private void handleArgs(Bundle args) { if (args == null) { return; } mApiUid = args.getLong(KEY_API_UID, -1L); mApiKey = args.getString(KEY_API_KEY); mGid = args.getLong(KEY_GID, -1L); mToken = args.getString(KEY_TOKEN, null); Parcelable[] parcelables = args.getParcelableArray(KEY_COMMENTS); if (parcelables instanceof GalleryComment[]) { mComments = (GalleryComment[]) parcelables; } }
mPendingRequest = savedInstanceState.getInt(KEY_INSTANCE_STATE_PENDING_REQUEST); mExistingAccounts = savedInstanceState.getParcelableArray(KEY_INSTANCE_STATE_EXISTING_ACCOUNTS);
when(bundle.getParcelableArray(anyString())).thenAnswer(get);
private void onRestore(@NonNull Bundle savedInstanceState) { mApiUid = savedInstanceState.getLong(KEY_API_UID, -1L); mApiKey = savedInstanceState.getString(KEY_API_KEY); mGid = savedInstanceState.getLong(KEY_GID, -1L); mToken = savedInstanceState.getString(KEY_TOKEN, null); Parcelable[] parcelables = savedInstanceState.getParcelableArray(KEY_COMMENTS); if (parcelables instanceof GalleryComment[]) { mComments = (GalleryComment[]) parcelables; } }
@Test public void getWrongType() { bundle.putFloat("foo", 5f); assertThat(bundle.getCharArray("foo")).isNull(); assertThat(bundle.getInt("foo")).isEqualTo(0); assertThat(bundle.getIntArray("foo")).isNull(); assertThat(bundle.getIntegerArrayList("foo")).isNull(); assertThat(bundle.getShort("foo")).isEqualTo((short) 0); assertThat(bundle.getShortArray("foo")).isNull(); assertThat(bundle.getBoolean("foo")).isFalse(); assertThat(bundle.getBooleanArray("foo")).isNull(); assertThat(bundle.getLong("foo")).isEqualTo(0); assertThat(bundle.getLongArray("foo")).isNull(); assertThat(bundle.getFloatArray("foo")).isNull(); assertThat(bundle.getDouble("foo")).isEqualTo(0.0); assertThat(bundle.getDoubleArray("foo")).isNull(); assertThat(bundle.getString("foo")).isNull(); assertThat(bundle.getStringArray("foo")).isNull(); assertThat(bundle.getStringArrayList("foo")).isNull(); assertThat(bundle.getBundle("foo")).isNull(); assertThat((Parcelable) bundle.getParcelable("foo")).isNull(); assertThat(bundle.getParcelableArray("foo")).isNull(); assertThat(bundle.getParcelableArrayList("foo")).isNull(); bundle.putInt("foo", 1); assertThat(bundle.getFloat("foo")).isEqualTo(0.0f); }
Parcelable[] value = bundle.getParcelableArray(key); if (value == null) { return null;
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); softwareComponents = (SoftwareComponent[]) getArguments().getParcelableArray(ARG_COMPONENTS); // Sort components by name Arrays.sort(softwareComponents, new Comparator<SoftwareComponent>() { @Override public int compare(SoftwareComponent o1, SoftwareComponent o2) { return o1.getName().compareTo(o2.getName()); } }); }
@Override public void restoreState(Parcelable state, ClassLoader loader) { if (state != null) { Bundle bundle = (Bundle) state; Parcelable[] states = bundle.getParcelableArray("states"); savedStates = new EmojiconGridView.SavedState[states.length]; for (int i = 0; i < states.length; i++) { savedStates[i] = (EmojiconGridView.SavedState) states[i]; } } super.restoreState(state, loader); }
/** * 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. * * @param bundle {@link Bundle} instance * @param key key in bundle * @param classType type of element in array * @param classArray type of elements array * @return array or null */ @Nullable @SuppressWarnings("unchecked") static <T extends Parcelable> T[] getParcelableArray(@NonNull Bundle bundle, @NonNull String key, Class<T> classType, Class<T[]> classArray) { Parcelable[] parcelableArray = bundle.getParcelableArray(key); if (classArray.isInstance(parcelableArray)) { return (T[]) parcelableArray; } else if (parcelableArray != null) { int size = parcelableArray.length; T[] items = (T[]) Array.newInstance(classType, size); int index = 0; for (Parcelable parcelable : parcelableArray) { if (classType.isInstance(parcelable)) { items[index++] = (T) parcelable; } } return items; } return null; } }
@Override public void restoreState(Parcelable state, ClassLoader loader) { if (state != null) { Bundle bundle = (Bundle) state; bundle.setClassLoader(loader); Parcelable[] fss = bundle.getParcelableArray("states"); mSavedState.clear(); mFragments.clear(); if (fss != null) { for (int i = 0; i < fss.length; i++) { mSavedState.add((Fragment.SavedState) fss[i]); } } Iterable<String> keys = bundle.keySet(); for (String key : keys) { if (key.startsWith("f")) { int index = Integer.parseInt(key.substring(1)); Fragment f = mFragmentManager.getFragment(bundle, key); if (f != null) { while (mFragments.size() <= index) { mFragments.add(null); } f.setMenuVisibility(false); mFragments.set(index, f); } } } } } }
@Override public void onViewCreated(View view, Bundle savedInstanceState) { GridView gridView = (GridView) view.findViewById(R.id.Emoji_GridView); Bundle bundle = getArguments(); if (bundle == null) { mEmojiconType = Emojicon.TYPE_UNDEFINED; mEmojicons = People.DATA; mUseSystemDefault = false; } else { //noinspection WrongConstant mEmojiconType = bundle.getInt(ARG_EMOJICON_TYPE); if (mEmojiconType == Emojicon.TYPE_UNDEFINED) { Parcelable[] parcels = bundle.getParcelableArray(ARG_EMOJICONS); mEmojicons = new Emojicon[parcels.length]; for (int i = 0; i < parcels.length; i++) { mEmojicons[i] = (Emojicon) parcels[i]; } } else { mEmojicons = Emojicon.getEmojicons(mEmojiconType); } mUseSystemDefault = bundle.getBoolean(ARG_USE_SYSTEM_DEFAULTS); } gridView.setAdapter(new EmojiconAdapter(view.getContext(), mEmojicons, mUseSystemDefault)); gridView.setOnItemClickListener(this); }
@NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Bundle args = getArguments(); final AspectRatio[] ratios = (AspectRatio[]) args.getParcelableArray(ARG_ASPECT_RATIOS); if (ratios == null) { throw new RuntimeException("No ratios"); } Arrays.sort(ratios); final AspectRatio current = args.getParcelable(ARG_CURRENT_ASPECT_RATIO); final AspectRatioAdapter adapter = new AspectRatioAdapter(ratios, current); return new AlertDialog.Builder(getActivity()) .setAdapter(adapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int position) { mListener.onAspectRatioSelected(ratios[position]); } }) .create(); }
Bundle bundle = (Bundle)state; bundle.setClassLoader(loader); Parcelable[] fss = bundle.getParcelableArray("states"); mSavedState.clear(); mFragments.clear();
@Override protected void onSceneResult(int requestCode, int resultCode, Bundle data) { switch (requestCode) { case REQUEST_CODE_COMMENT_GALLERY: if (resultCode != RESULT_OK || data == null){ break; } Parcelable[] array = data.getParcelableArray(GalleryCommentsScene.KEY_COMMENTS); if (!(array instanceof GalleryComment[])) { break; } GalleryComment[] comments = (GalleryComment[]) array; if (mGalleryDetail == null) { break; } mGalleryDetail.comments = comments; bindComments(comments); break; default: super.onSceneResult(requestCode, resultCode, data); } }
@Test public void parcelableArray() { Bundle innerBundle = new Bundle(); innerBundle.putInt("value", 1); Parcelable[] arr = new Parcelable[] { innerBundle }; bundle.putParcelableArray("foo", arr); assertThat(bundle.getParcelableArray("foo")).isEqualTo(arr); assertThat(bundle.getParcelableArray("bar")).isNull(); } }
Bundle bundle = getIntent().getExtras(); Parcelable[] parcels = bundle.getParcelableArray(INTENT_EXTRA_ADDRESSES); Address[] addresses = new Address[parcels.length]; for (Parcelable par : parcels){ addresses.add((Address) par); }
@Test public void testWriteToBundle_contentUriTrigger() { ObservedUri observedUri = new ObservedUri(ContactsContract.AUTHORITY_URI, Flags.FLAG_NOTIFY_FOR_DESCENDANTS); ContentUriTrigger contentUriTrigger = Trigger.contentUriTrigger(Arrays.asList(observedUri)); Bundle bundle = writer.writeToBundle( initializeDefaultBuilder().setTrigger(contentUriTrigger).build(), new Bundle()); Uri[] uris = (Uri[]) bundle.getParcelableArray(BundleProtocol.PACKED_PARAM_CONTENT_URI_ARRAY); int[] flags = bundle.getIntArray(BundleProtocol.PACKED_PARAM_CONTENT_URI_FLAGS_ARRAY); assertTrue("Array size", uris.length == flags.length && flags.length == 1); assertEquals( BundleProtocol.PACKED_PARAM_CONTENT_URI_ARRAY, ContactsContract.AUTHORITY_URI, uris[0]); assertEquals( BundleProtocol.PACKED_PARAM_CONTENT_URI_FLAGS_ARRAY, Flags.FLAG_NOTIFY_FOR_DESCENDANTS, flags[0]); }
@Override @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { final Account[] accounts = (Account[]) getArguments().getParcelableArray(ACCOUNTS); final int[] checkedItem = {0}; final ColorStateList colorStateList = ContextCompat.getColorStateList(getContext(), R.color.secondary_text); return new AlertDialog.Builder(getContext()) .setTitle(R.string.select_google_account) .setIcon(getTinted(getResources(), R.drawable.ic_account_circle, colorStateList.getDefaultColor())) .setSingleChoiceItems(getNames(accounts), checkedItem[0], new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { checkedItem[0] = which; } }) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int which) { ((AccountManagerAuthActivity)getActivity()).onAccountSelected(accounts[checkedItem[0]]); } }) .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { ((AccountManagerAuthActivity)getActivity()).onCanceled(); } }) .create(); }
/** * Retrieve extended data from the request. * * @param name The name of the desired item. * @return the value of an item that previously added with putExtra() or * null if no Parcelable[] value was found. * @see #putExtra(String, Parcelable[]) */ public Parcelable[] getParcelableArrayExtra(String name) { return mExtras == null ? null : mExtras.getParcelableArray(name); }
@Override public Bundle call(String method, String args, Bundle extras) { if (method.equals("customInsert")) { ContentValues[] values = extras.getParcelableArray("contentValues") // do bulk insert and return list of ids in a new Bundle } }