下面列出了android.os.Bundle#putSparseParcelableArray ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
Bundle toBundle(KeyParceler parceler) {
Bundle outState = new Bundle();
outState.putParcelable(KEY, parceler.toParcelable(getKey()));
int[] viewIds = new int[viewStateById.size()];
int c = 0;
for (Map.Entry<Integer, SparseArray<Parcelable>> entry : viewStateById.entrySet()) {
Integer viewId = entry.getKey();
viewIds[c++] = viewId;
SparseArray<Parcelable> viewState = entry.getValue();
if (viewState.size() > 0) {
outState.putSparseParcelableArray(VIEW_STATE_PREFIX + viewId, viewState);
}
}
outState.putIntArray(VIEW_STATE_IDS, viewIds);
if (bundle != null && !bundle.isEmpty()) {
outState.putBundle(BUNDLE, bundle);
}
return outState;
}
public void saveActionViewStates(Bundle outStates) {
SparseArray<Parcelable> viewStates = null;
final int itemCount = size();
for (int i = 0; i < itemCount; i++) {
final MenuItem item = getItem(i);
final View v = item.getActionView();
if (v != null && v.getId() != View.NO_ID) {
if (viewStates == null) {
viewStates = new SparseArray<Parcelable>();
}
v.saveHierarchyState(viewStates);
if (item.isActionViewExpanded()) {
outStates.putInt(EXPANDED_ACTION_VIEW_ID, item.getItemId());
}
}
if (item.hasSubMenu()) {
final SubMenuBuilder subMenu = (SubMenuBuilder) item.getSubMenu();
subMenu.saveActionViewStates(outStates);
}
}
if (viewStates != null) {
outStates.putSparseParcelableArray(getActionViewStatesKey(), viewStates);
}
}
private void dispatchSaveInstanceState(Bundle outState) {
if (mPresenters.isEmpty()) return;
SparseArray<Parcelable> presenterStates = new SparseArray<Parcelable>();
for (WeakReference<MenuPresenter> ref : mPresenters) {
final MenuPresenter presenter = ref.get();
if (presenter == null) {
mPresenters.remove(ref);
} else {
final int id = presenter.getId();
if (id > 0) {
final Parcelable state = presenter.onSaveInstanceState();
if (state != null) {
presenterStates.put(id, state);
}
}
}
}
outState.putSparseParcelableArray(PRESENTER_KEY, presenterStates);
}
public void saveActionViewStates(Bundle outStates) {
SparseArray<Parcelable> viewStates = null;
final int itemCount = size();
for (int i = 0; i < itemCount; i++) {
final MenuItem item = getItem(i);
final View v = item.getActionView();
if (v != null && v.getId() != View.NO_ID) {
if (viewStates == null) {
viewStates = new SparseArray<Parcelable>();
}
v.saveHierarchyState(viewStates);
if (item.isActionViewExpanded()) {
outStates.putInt(EXPANDED_ACTION_VIEW_ID, item.getItemId());
}
}
if (item.hasSubMenu()) {
final SubMenuBuilder subMenu = (SubMenuBuilder) item.getSubMenu();
subMenu.saveActionViewStates(outStates);
}
}
if (viewStates != null) {
outStates.putSparseParcelableArray(getActionViewStatesKey(), viewStates);
}
}
private void dispatchSaveInstanceState(Bundle outState) {
if (mPresenters.isEmpty()) return;
SparseArray<Parcelable> presenterStates = new SparseArray<Parcelable>();
for (WeakReference<MenuPresenter> ref : mPresenters) {
final MenuPresenter presenter = ref.get();
if (presenter == null) {
mPresenters.remove(ref);
} else {
final int id = presenter.getId();
if (id > 0) {
final Parcelable state = presenter.onSaveInstanceState();
if (state != null) {
presenterStates.put(id, state);
}
}
}
}
outState.putSparseParcelableArray(PRESENTER_KEY, presenterStates);
}
@NonNull
@Override
public Parcelable onSaveInstanceState() {
final Bundle state = new Bundle();
if (menuView != null) {
SparseArray<Parcelable> hierarchy = new SparseArray<>();
menuView.saveHierarchyState(hierarchy);
state.putSparseParcelableArray(STATE_HIERARCHY, hierarchy);
}
if (adapter != null) {
state.putBundle(STATE_ADAPTER, adapter.createInstanceState());
}
if (headerLayout != null) {
SparseArray<Parcelable> header = new SparseArray<>();
headerLayout.saveHierarchyState(header);
state.putSparseParcelableArray(STATE_HEADER, header);
}
return state;
}
@NonNull
public Bundle createInstanceState() {
Bundle state = new Bundle();
if (checkedItem != null) {
state.putInt(STATE_CHECKED_ITEM, checkedItem.getItemId());
}
// Store the states of the action views.
SparseArray<ParcelableSparseArray> actionViewStates = new SparseArray<>();
for (int i = 0, size = items.size(); i < size; i++) {
NavigationMenuItem navigationMenuItem = items.get(i);
if (navigationMenuItem instanceof NavigationMenuTextItem) {
MenuItemImpl item = ((NavigationMenuTextItem) navigationMenuItem).getMenuItem();
View actionView = item != null ? item.getActionView() : null;
if (actionView != null) {
ParcelableSparseArray container = new ParcelableSparseArray();
actionView.saveHierarchyState(container);
actionViewStates.put(item.getItemId(), container);
}
}
}
state.putSparseParcelableArray(STATE_ACTION_VIEWS, actionViewStates);
return state;
}
@Override
protected Parcelable onSaveInstanceState() {
Log.d(TAG, "onSaveInstanceState");
Bundle bundle = new Bundle();
bundle.putParcelable("superState", super.onSaveInstanceState());
//STORE CUSTOM VALUES
bundle.putSparseParcelableArray(PROFILE_LIST, profileSparseArray);
return bundle;
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
if (drawerView != null) {
drawerViewState = new SparseArray<>();
drawerView.saveHierarchyState(drawerViewState);
outState.putSparseParcelableArray(KEY_DRAWER_VIEW_STATE, drawerViewState);
}
}
@Override
public Parcelable saveState() {
Bundle bundle = new Bundle();
for (ViewHolder viewHolder : getAttachedViewHolders()) {
mSavedStates.put(getItemId(viewHolder.mPosition), viewHolder.onSaveInstanceState());
}
bundle.putSparseParcelableArray(STATE, mSavedStates);
return bundle;
}
private Parcelable onSaveInstanceState() {
SparseArray<Parcelable> state = new SparseArray<>();
itemView.saveHierarchyState(state);
Bundle bundle = new Bundle();
bundle.putSparseParcelableArray(STATE, state);
return bundle;
}
/**
* @return the saved views states
*/
public final Bundle saveAsBundle() {
if (mChildStates == null || mChildStates.size() == 0) {
return null;
}
Map<String, SparseArray<Parcelable>> snapshot = mChildStates.snapshot();
Bundle bundle = new Bundle();
for (Iterator<Entry<String, SparseArray<Parcelable>>> i =
snapshot.entrySet().iterator(); i.hasNext(); ) {
Entry<String, SparseArray<Parcelable>> e = i.next();
bundle.putSparseParcelableArray(e.getKey(), e.getValue());
}
return bundle;
}
@Override
public void writeToParcel(final Parcel parcel, final int arg1) {
final Bundle b = new Bundle();
b.putString(LOCAL_NAME_COMPLETE, mLocalNameComplete);
b.putString(LOCAL_NAME_SHORT, mLocalNameShort);
b.putSparseParcelableArray(RECORDS_ARRAY, mAdRecords);
parcel.writeBundle(b);
}
@Override
public Parcelable saveState() {
Bundle bundle = new Bundle();
for (ViewHolder viewHolder : getAttachedViewHolders()) {
mSavedStates.put(getItemId(viewHolder.mPosition), viewHolder.onSaveInstanceState());
}
bundle.putSparseParcelableArray(STATE, mSavedStates);
return bundle;
}
@Override
public void writeToParcel(final Parcel parcel, final int arg1) {
final Bundle b = new Bundle();
b.putString("local_name_complete", mLocalNameComplete);
b.putString("local_name_short", mLocalNameShort);
b.putSparseParcelableArray("records_array", mAdRecords);
parcel.writeBundle(b);
}
/**
* data is sparsearray
* @param activity Activity
* @param channel 渠道
* @param data data
* @param requestCode requestCode
*/
public static void showShareDialog(Activity activity, int channel, SparseArray<ShareEntity> data, int requestCode) {
if (null == activity || activity.isFinishing()) {
return;
}
Intent intent = new Intent(activity, ShareDialogActivity.class);
Bundle bundle = new Bundle();
bundle.putSparseParcelableArray(ShareConstant.EXTRA_SHARE_DATA, data);
intent.putExtra(ShareConstant.EXTRA_SHARE_DATA, bundle);
intent.putExtra(ShareConstant.EXTRA_SHARE_CHANNEL, channel);
activity.startActivityForResult(intent, requestCode);
}
public static void putValueToBundle(
@NonNull Bundle bundle, @NonNull String key, @NonNull Object value) {
if (value instanceof String) {
bundle.putString(key, (String) value);
} else if (value instanceof Integer) {
bundle.putInt(key, (int) value);
} else if (value instanceof Boolean) {
bundle.putBoolean(key, (boolean) value);
} else if (value instanceof Long) {
bundle.putLong(key, (long) value);
} else if (value instanceof Short) {
bundle.putShort(key, (short) value);
} else if (value instanceof Double) {
bundle.putDouble(key, (double) value);
} else if (value instanceof Float) {
bundle.putFloat(key, (float) value);
} else if (value instanceof Character) {
bundle.putChar(key, (char) value);
} else if (value instanceof Byte) {
bundle.putByte(key, (byte) value);
} else if (value instanceof CharSequence) {
bundle.putCharSequence(key, (CharSequence) value);
} else if (value instanceof Bundle) {
bundle.putBundle(key, (Bundle) value);
} else if (value instanceof Parcelable) {
bundle.putParcelable(key, (Parcelable) value);
} else if (value instanceof String[]) {
bundle.putStringArray(key, (String[]) value);
} else if (value instanceof int[]) {
bundle.putIntArray(key, (int[]) value);
} else if (value instanceof boolean[]) {
bundle.putBooleanArray(key, (boolean[]) value);
} else if (value instanceof long[]) {
bundle.putLongArray(key, (long[]) value);
} else if (value instanceof short[]) {
bundle.putShortArray(key, (short[]) value);
} else if (value instanceof double[]) {
bundle.putDoubleArray(key, (double[]) value);
} else if (value instanceof float[]) {
bundle.putFloatArray(key, (float[]) value);
} else if (value instanceof char[]) {
bundle.putCharArray(key, (char[]) value);
} else if (value instanceof byte[]) {
bundle.putByteArray(key, (byte[]) value);
} else if (value instanceof CharSequence[]) {
bundle.putCharSequenceArray(key, (CharSequence[]) value);
} else if (value instanceof Parcelable[]) {
bundle.putParcelableArray(key, (Parcelable[]) value);
} else if (value instanceof ArrayList) {
bundle.putIntegerArrayList(key, (ArrayList<Integer>) value);
} else if (value instanceof SparseArray) {
bundle.putSparseParcelableArray(key, (SparseArray<? extends Parcelable>) value);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
if (value instanceof IBinder) {
bundle.putBinder(key, (IBinder) value);
return;
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (value instanceof Size) {
bundle.putSize(key, (Size) value);
return;
} else if (value instanceof SizeF) {
bundle.putSizeF(key, (SizeF) value);
return;
}
}
if (value instanceof Serializable) {
bundle.putSerializable(key, (Serializable) value);
return;
}
throw new RuntimeException(String.format(Locale.getDefault(),
"Arguments extra %s has wrong type %s.", key, value.getClass().getName()));
}
}
@Override
public final void saveInstanceState(@NonNull final Bundle outState) {
outState.putSparseParcelableArray(SAVED_INSTANCE_STATES_EXTRA, savedInstanceStates);
}
public void onSaveInstanceState(Bundle outState) {
outState.putSparseParcelableArray(OPENED_STATES, openedPositions);
}
@Override public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(screen.ordinal());
Bundle bundle = new Bundle();
bundle.putSparseParcelableArray("viewState", viewState);
dest.writeBundle(bundle);
}