下面列出了android.os.Bundle#getSparseParcelableArray ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void dispatchRestoreInstanceState(Bundle state) {
SparseArray<Parcelable> presenterStates = state.getSparseParcelableArray(PRESENTER_KEY);
if (presenterStates == null || mPresenters.isEmpty()) return;
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) {
Parcelable parcel = presenterStates.get(id);
if (parcel != null) {
presenter.onRestoreInstanceState(parcel);
}
}
}
}
}
public final View createDrawerView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
drawerView = onCreateDrawerView(inflater, container, savedInstanceState);
if (drawerView != null) {
SparseArray<Parcelable> saved = drawerViewState;
if (saved == null && savedInstanceState != null) {
saved = savedInstanceState.getSparseParcelableArray(KEY_DRAWER_VIEW_STATE);
}
if (saved != null) {
drawerView.restoreHierarchyState(saved);
}
}
return drawerView;
}
private void dispatchRestoreInstanceState(Bundle state) {
SparseArray<Parcelable> presenterStates = state.getSparseParcelableArray(PRESENTER_KEY);
if (presenterStates == null || mPresenters.isEmpty()) return;
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) {
Parcelable parcel = presenterStates.get(id);
if (parcel != null) {
presenter.onRestoreInstanceState(parcel);
}
}
}
}
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
Log.d(TAG, "onRestoreInstanceState");
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
state = bundle.getParcelable("superState");
//RESTORE CUSTOM VALUES
profileSparseArray = bundle.getSparseParcelableArray(PROFILE_LIST) == null ? new SparseArray<>() : bundle.getSparseParcelableArray(PROFILE_LIST);
populateAvatar();
}
if (hvFragmentManager != null) {
ProfileChooserFragment profileChooserFragment = (ProfileChooserFragment) hvFragmentManager.findFragmentByTag(ProfileChooserFragment.FRAGMENT_TAG);
if (profileChooserFragment != null) {
profileChooserFragment.setCallback(HeaderView.this);
profileChooserFragment.updateTypeface(typeface);
}
}
super.onRestoreInstanceState(state);
}
/**
* Retrieves the instance of {@link CustomTabColorSchemeParams} from an Intent for a given
* color scheme. Uses values passed directly into {@link CustomTabsIntent.Builder} (e.g. via
* {@link Builder#setToolbarColor}) as defaults.
*
* @param intent Intent to retrieve the color scheme parameters from.
* @param colorScheme A constant representing a color scheme. Should not be
* {@link #COLOR_SCHEME_SYSTEM}.
* @return An instance of {@link CustomTabColorSchemeParams} with retrieved parameters.
*/
@NonNull
public static CustomTabColorSchemeParams getColorSchemeParams(@NonNull Intent intent,
@ColorScheme int colorScheme) {
if (colorScheme < 0 || colorScheme > COLOR_SCHEME_MAX
|| colorScheme == COLOR_SCHEME_SYSTEM) {
throw new IllegalArgumentException("Invalid colorScheme: " + colorScheme);
}
Bundle extras = intent.getExtras();
if (extras == null) {
return CustomTabColorSchemeParams.fromBundle(null);
}
CustomTabColorSchemeParams defaults = CustomTabColorSchemeParams.fromBundle(extras);
SparseArray<Bundle> paramBundles = extras.getSparseParcelableArray(
EXTRA_COLOR_SCHEME_PARAMS);
if (paramBundles != null) {
Bundle bundleForScheme = paramBundles.get(colorScheme);
if (bundleForScheme != null) {
return CustomTabColorSchemeParams.fromBundle(bundleForScheme)
.withDefaults(defaults);
}
}
return defaults;
}
private void dispatchRestoreInstanceState(Bundle state) {
SparseArray<Parcelable> presenterStates = state.getSparseParcelableArray(PRESENTER_KEY);
if (presenterStates == null || mPresenters.isEmpty()) return;
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) {
Parcelable parcel = presenterStates.get(id);
if (parcel != null) {
presenter.onRestoreInstanceState(parcel);
}
}
}
}
}
public final View createDrawerView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
drawerView = onCreateDrawerView(inflater, container, savedInstanceState);
if (drawerView != null) {
SparseArray<Parcelable> saved = drawerViewState;
if (saved == null && savedInstanceState != null) {
saved = savedInstanceState.getSparseParcelableArray(KEY_DRAWER_VIEW_STATE);
}
if (saved != null) {
drawerView.restoreHierarchyState(saved);
}
}
return drawerView;
}
@Override
public void onRestoreInstanceState(final Parcelable parcelable) {
if (parcelable instanceof Bundle) {
Bundle state = (Bundle) parcelable;
SparseArray<Parcelable> hierarchy = state.getSparseParcelableArray(STATE_HIERARCHY);
if (hierarchy != null) {
menuView.restoreHierarchyState(hierarchy);
}
Bundle adapterState = state.getBundle(STATE_ADAPTER);
if (adapterState != null) {
adapter.restoreInstanceState(adapterState);
}
SparseArray<Parcelable> header = state.getSparseParcelableArray(STATE_HEADER);
if (header != null) {
headerLayout.restoreHierarchyState(header);
}
}
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
bundle.setClassLoader(loader);
SparseArray<Parcelable> ss = bundle.containsKey(STATE) ? bundle.getSparseParcelableArray(STATE) : null;
mSavedStates = ss != null ? ss : new SparseArray<Parcelable>();
}
super.restoreState(state, loader);
}
public void restoreActionViewStates(Bundle states) {
if (states == null) {
return;
}
SparseArray<Parcelable> viewStates = states.getSparseParcelableArray(
getActionViewStatesKey());
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB && viewStates == null) {
//Fixes Issue #652 with sdk <= 2.3.6
return;
}
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) {
v.restoreHierarchyState(viewStates);
}
if (item.hasSubMenu()) {
final SubMenuBuilder subMenu = (SubMenuBuilder) item.getSubMenu();
subMenu.restoreActionViewStates(states);
}
}
final int expandedId = states.getInt(EXPANDED_ACTION_VIEW_ID);
if (expandedId > 0) {
MenuItem itemToExpand = findItem(expandedId);
if (itemToExpand != null) {
itemToExpand.expandActionView();
}
}
}
@Override
public final void restoreInstanceState(@Nullable final Bundle savedInstanceState) {
if (savedInstanceState != null) {
savedInstanceStates =
savedInstanceState.getSparseParcelableArray(SAVED_INSTANCE_STATES_EXTRA);
}
}
public static TestClassBundled unbundle(Bundle bundle, Gson gson) {
return new AutoValue_TestClassBundled(
bundle,
bundle.getByte("some_byte"),
bundle.getBoolean("some_boolean"),
bundle.getShort("some_short"),
bundle.getInt("some_int"),
bundle.getLong("some_long"),
bundle.getChar("some_char"),
bundle.getFloat("some_float"),
bundle.getDouble("some_double"),
bundle.getString("some_string"),
bundle.getCharSequence("some_char_sequence"),
bundle.getParcelable("some_parcelable"),
bundle.getParcelableArrayList("some_parcelable_array_list"),
bundle.getSparseParcelableArray("some_parcelable_sparse_array"),
bundle.getSerializable("some_serializable"),
bundle.getIntegerArrayList("some_integer_array_list"),
bundle.getStringArrayList("some_string_array_list"),
bundle.getCharSequenceArrayList("some_char_sequence_array_list"),
bundle.getByteArray("some_byte_array"),
bundle.getShortArray("some_short_array"),
bundle.getCharArray("some_char_array"),
bundle.getFloatArray("some_float_array"),
gson.fromJson(bundle.getString("some_unknown_object"), new com.google.common.reflect.TypeToken<UnknownObject>(){}.getType()),
gson.fromJson(bundle.getString("some_unknown_object_list"), new com.google.common.reflect.TypeToken<ArrayList<UnknownObject>>(){}.getType()),
gson.fromJson(bundle.getString("test_enum"), new com.google.common.reflect.TypeToken<TestEnum>(){}.getType()));
}
@NonNull
static State fromBundle(@NonNull Bundle savedState, @NonNull KeyParceler parceler) {
Object key = parceler.toKey(savedState.getParcelable("KEY"));
State state = new State(key);
state.viewState = savedState.getSparseParcelableArray("VIEW_STATE");
state.bundle = savedState.getBundle("BUNDLE");
return state;
}
public void restoreActionViewStates(Bundle states) {
if (states == null) {
return;
}
SparseArray<Parcelable> viewStates = states.getSparseParcelableArray(
getActionViewStatesKey());
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB && viewStates == null) {
//Fixes Issue #652 with sdk <= 2.3.6
return;
}
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) {
v.restoreHierarchyState(viewStates);
}
if (item.hasSubMenu()) {
final SubMenuBuilder subMenu = (SubMenuBuilder) item.getSubMenu();
subMenu.restoreActionViewStates(states);
}
}
final int expandedId = states.getInt(EXPANDED_ACTION_VIEW_ID);
if (expandedId > 0) {
MenuItem itemToExpand = findItem(expandedId);
if (itemToExpand != null) {
itemToExpand.expandActionView();
}
}
}
private static Entry fromBundle(Bundle bundle, StackableParceler parceler) {
StackablePath path = parceler.unwrap(bundle.getParcelable(PATH_KEY));
// new entry with new scope instance, but preserving previous scope name
Entry entry = new Entry(bundle.getString(SCOPE_KEY), path, bundle.getInt(NAV_TYPE_KEY));
entry.state = bundle.getSparseParcelableArray(STATE_KEY);
return entry;
}
@NonNull static State fromBundle(@NonNull Bundle savedState, @NonNull KeyParceler parceler) {
Object key = parceler.toKey(savedState.getParcelable(KEY));
State state = new State(key);
int[] viewIds = checkNotNull(savedState.getIntArray(VIEW_STATE_IDS), "Null view state ids?");
for (int viewId : viewIds) {
SparseArray<Parcelable> viewState =
savedState.getSparseParcelableArray(VIEW_STATE_PREFIX + viewId);
if (viewState != null) {
state.viewStateById.put(viewId, viewState);
}
}
state.bundle = savedState.getBundle(BUNDLE);
return state;
}
private void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
SparseArray<Parcelable> ss = bundle.containsKey(STATE) ? bundle.getSparseParcelableArray(STATE) : null;
if (ss != null) {
itemView.restoreHierarchyState(ss);
}
}
}
final void restore(Bundle savedInstanceState) {
if (viewState == null && savedInstanceState != null) {
viewState = savedInstanceState.getSparseParcelableArray(VIEW_STATE + hashCode());
}
}
@Override public BackstackFrame createFromParcel(Parcel source) {
Screen screen = Screen.values()[source.readInt()];
Bundle bundle = source.readBundle();
SparseArray<Parcelable> viewState = bundle.getSparseParcelableArray("viewState");
return new BackstackFrame(screen, viewState);
}
public void restoreInstanceState(Bundle savedInstanceState) {
openedPositions = savedInstanceState.getSparseParcelableArray(OPENED_STATES);
if (openedPositions == null) {
openedPositions = new SparseArray<>();
}
}