下面列出了怎么用android.os.Parcelable的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void onRestoreInstanceState(Parcelable state)
{
if (!state.getClass().equals(SavedState.class)) {
// Didn't save state for us in onSaveInstanceState
super.onRestoreInstanceState(state);
int value = this.value;
if (fragment != null)
fragment.chromaColorView.setCurrentColor(value);
setSummaryCCDP(value);
return;
}
// restore instance state
CustomColorDialogPreferenceX.SavedState myState = (CustomColorDialogPreferenceX.SavedState)state;
super.onRestoreInstanceState(myState.getSuperState());
value = myState.value;
defaultValue = myState.defaultValue;
if (fragment != null)
fragment.chromaColorView.setCurrentColor(value);
setSummaryCCDP(value);
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
Bundle bundle = (Bundle) state;
showTimeBattery = bundle.getBoolean("showTimeBattery");
batteryLevel = bundle.getInt("batteryLevel", batteryLevel);
durChapterName = bundle.getString("durChapterName");
durPage = bundle.getInt("durPage");
pageSize = bundle.getInt("pageSize");
durChapter = bundle.getInt("durChapter");
chapterSize = bundle.getInt("chapterSize");
super.onRestoreInstanceState(bundle.getParcelable("super_data"));
updateTime();
setShowTimeBattery(showTimeBattery);
updatePageIndex(durChapterName, durPage, pageSize);
updateChapterIndex(durChapter, chapterSize);
}
@Override
protected Parcelable onSaveInstanceState() {
final Bundle bundle = new Bundle();
bundle.putParcelable(INSTANCE_STATE, super.onSaveInstanceState());
bundle.putInt(INSTANCE_TEXT_COLOR, getTextColor());
bundle.putFloat(INSTANCE_TEXT_SIZE, getProgressTextSize());
bundle.putFloat(INSTANCE_REACHED_BAR_HEIGHT, getReachedBarHeight());
bundle.putFloat(INSTANCE_UNREACHED_BAR_HEIGHT, getUnreachedBarHeight());
bundle.putInt(INSTANCE_REACHED_BAR_COLOR, getReachedBarColor());
bundle.putInt(INSTANCE_UNREACHED_BAR_COLOR, getUnreachedBarColor());
bundle.putInt(INSTANCE_MAX, getMax());
bundle.putInt(INSTANCE_PROGRESS, getProgress());
bundle.putString(INSTANCE_SUFFIX, getSuffix());
bundle.putString(INSTANCE_PREFIX, getPrefix());
bundle.putBoolean(INSTANCE_TEXT_VISIBILITY, getProgressTextVisibility());
return bundle;
}
/**
* 创建快捷方式
*
* @param cxt
* Context
* @param icon
* 快捷方式图标
* @param title
* 快捷方式标题
* @param cls
* 要启动的Activity类
*/
public static void createDeskShortCut(Context cxt, int icon, String title,
Class<?> cls) {
// 创建快捷方式的Intent
Intent shortcutIntent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
// 不允许重复创建
shortcutIntent.putExtra("duplicate", false);
// 需要现实的名称
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
// 快捷图片
Parcelable ico = Intent.ShortcutIconResource.fromContext(
cxt.getApplicationContext(), icon);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, ico);
Intent intent = new Intent(cxt, cls);
// 下面两个属性是为了当应用程序卸载时桌面上的快捷方式会删除
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
// 点击快捷图片,运行的程序主入口
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
// 发送广播。OK
cxt.sendBroadcast(shortcutIntent);
}
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);
}
/**
* Parcelable interface: procedure reads the formula state
*/
@Override
public void onRestoreInstanceState(Parcelable state)
{
if (state == null)
{
return;
}
if (state instanceof Bundle)
{
Bundle bundle = (Bundle) state;
properties.assign((ResultProperties) bundle.getParcelable(STATE_RESULT_PROPERTIES));
super.onRestoreInstanceState(bundle);
updateResultView(false);
}
}
@Override
public void onRestoreInstanceState(Parcelable state) {
if (!(state instanceof GifViewSavedState)) {
super.onRestoreInstanceState(state);
return;
}
GifViewSavedState ss = (GifViewSavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
Drawable[] compoundDrawables = getCompoundDrawables();
ss.restoreState(compoundDrawables[0], 0);
ss.restoreState(compoundDrawables[1], 1);
ss.restoreState(compoundDrawables[2], 2);
ss.restoreState(compoundDrawables[3], 3);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Drawable[] compoundDrawablesRelative = getCompoundDrawablesRelative();
ss.restoreState(compoundDrawablesRelative[0], 4);
ss.restoreState(compoundDrawablesRelative[2], 5);
}
ss.restoreState(getBackground(), 6);
}
@Override protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState state = new SavedState(superState);
state.type = type;
state.showId = showId;
state.showTitle = showTitle;
state.episodeId = episodeId;
state.episodeTitle = episodeTitle;
state.movieId = movieId;
state.movieTitle = movieTitle;
state.movieOverview = movieOverview;
state.poster = poster;
state.startTime = startTime;
state.endTime = endTime;
state.startTime = startTime;
state.endTime = endTime;
state.isExpanded = isExpanded();
return state;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if(state instanceof Bundle){
final Bundle bundle = (Bundle)state;
mTextColor = bundle.getInt(INSTANCE_TEXT_COLOR);
mTextSize = bundle.getFloat(INSTANCE_TEXT_SIZE);
mReachedBarHeight = bundle.getFloat(INSTANCE_REACHED_BAR_HEIGHT);
mUnreachedBarHeight = bundle.getFloat(INSTANCE_UNREACHED_BAR_HEIGHT);
mReachedBarColor = bundle.getInt(INSTANCE_REACHED_BAR_COLOR);
mUnreachedBarColor = bundle.getInt(INSTANCE_UNREACHED_BAR_COLOR);
initializePainters();
setMax(bundle.getInt(INSTANCE_MAX));
setProgress(bundle.getInt(INSTANCE_PROGRESS));
setPrefix(bundle.getString(INSTANCE_PREFIX));
setSuffix(bundle.getString(INSTANCE_SUFFIX));
setProgressTextVisibility(bundle.getBoolean(INSTANCE_TEXT_VISBILITY) ? ProgressTextVisibility.Visible : ProgressTextVisibility.Invisible);
super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATE));
return;
}
super.onRestoreInstanceState(state);
}
@Override
public Parcelable onSaveInstanceState(@NonNull CoordinatorLayout parent, @NonNull T abl) {
final Parcelable superState = super.onSaveInstanceState(parent, abl);
final int offset = getTopAndBottomOffset();
// Try and find the first visible child...
for (int i = 0, count = abl.getChildCount(); i < count; i++) {
View child = abl.getChildAt(i);
final int visBottom = child.getBottom() + offset;
if (child.getTop() + offset <= 0 && visBottom >= 0) {
final SavedState ss = new SavedState(superState);
ss.firstVisibleChildIndex = i;
ss.firstVisibleChildAtMinimumHeight =
visBottom == (ViewCompat.getMinimumHeight(child) + abl.getTopInset());
ss.firstVisibleChildPercentageShown = visBottom / (float) child.getHeight();
return ss;
}
}
// Else we'll just return the super state
return superState;
}
@Override
public boolean dispatchTouchEvent(@NonNull MotionEvent event) {
boolean wasLongPress = mGestureDetector.onTouchEvent(event);
if(!wasLongPress) {
Log.i("test", "not a long press");
if(!mDismissOverlayView.isShown()) {
Parcel p = Parcel.obtain();
event.writeToParcel(p, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
sendEvent(p.marshall());
} else {
return super.dispatchTouchEvent(event);
}
} else {
Log.i("test", "long press");
}
return true;
}
@Test
public void allParcelables() throws IllegalAccessException, InstantiationException, NoSuchFieldException {
ModelPopulator populator = new ModelPopulator("CREATOR", "$jacocoData");
for (Class<? extends Parcelable> modelClass : getModelClasses()) {
Parcelable instance = populator.populateWithRandomValues(modelClass);
testSingleParcelable(instance);
testParcelableArray(instance);
/* Trick to increase code coverage */
instance.describeContents();
((Parcelable.Creator<?>) modelClass.getField("CREATOR").get(null)).newArray(13);
}
}
@Nullable
@Override
protected Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
bundle.putParcelable(SUPER_INSTANCE_STATE, super.onSaveInstanceState());
return super.onSaveInstanceState();
}
@Override
public Parcelable onSaveInstanceState() {
// Force our ancestor class to save its state
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.progress = mProgress;
ss.secondaryProgress = mSecondaryProgress;
return ss;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
SavedState savedState = (SavedState) state;
super.onRestoreInstanceState(savedState.getSuperState());
//刷新状态
toggleStatus(savedState.status);
}
public EntryState(int formulaId, FormulaBase.BaseType type, Parcelable data)
{
super();
this.formulaId = formulaId;
this.type = type;
this.data = data;
}
public static Parcelable fetchParentState(Parcelable state) {
if(state == null) {
return null;
}
if(Utils.IS_AT_LEAST_MARSHMALLOW) {
return state;
} else {
return View.BaseSavedState.EMPTY_STATE;
}
}
private static void writeList(LinkedList<Object> parcelData, InvocationOnMock invocation, List<Parcelable> list) {
if (list == null) {
parcelData.add(-1);
} else {
parcelData.add(list.size());
for (Parcelable item : list) {
writeParcelable(parcelData, item, (Parcel) invocation.getMock());
}
}
}
@Override
public void onRestoreInstanceState(Parcelable state) {
SavedState savedState = (SavedState)state;
super.onRestoreInstanceState(savedState.getSuperState());
mCurrentPage = savedState.currentPage;
requestLayout();
}
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState savedState = new SavedState(superState);
savedState.currentPage = mCurrentPage;
return savedState;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
SavedState savedState = (SavedState)state;
super.onRestoreInstanceState(savedState.getSuperState());
mCurrentPage = savedState.currentPage;
requestLayout();
}
@Override
public void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
final Bundle savedState = (Bundle) state;
super.onRestoreInstanceState(savedState.getParcelable(STATE_KEY_SUPER));
setChecked(savedState.getBoolean(STATE_KEY_CHECKED), false);
}
}
@Override
protected Parcelable onSaveInstanceState() {
Parcelable parcelable = super.onSaveInstanceState();
SavedState ss = new SavedState(parcelable);
ss.selX = curX;
ss.selY = curY;
ss.color = bitmapForColor;
if (mIndicatorEnable) {
ss.indicator = bitmapForIndicator;
}
return ss;
}
@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.isShowingMenu = isMenuOpen();
return ss;
}
@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
if (currentDir == null) return superState;
Bundle dialogState = dialog == null ? null : dialog.onSaveInstanceState();
return new SavedStateHandler(superState, currentDir.getPath(), dialogState);
}
/**
* 存储当前状态
* @return
*/
@Override
public Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
bundle.putParcelable("superState", super.onSaveInstanceState());
bundle.putSerializable(TAG, mState);
return bundle;
}
@Override
public Parcelable onSaveInstanceState() {
if (mPendingSavedState != null) {
return new SavedState(mPendingSavedState);
}
SavedState savedState = new SavedState();
savedState.position = mPendingScrollPosition;
savedState.offset = mOffset;
savedState.isReverseLayout = mShouldReverseLayout;
return savedState;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
Bundle savedState = (Bundle) state;
Parcelable superState = savedState.getParcelable(STATE_PARENT);
super.onRestoreInstanceState(superState);
setColor(Color.HSVToColor(savedState.getFloatArray(STATE_COLOR)));
if (savedState.containsKey(STATE_SATURATION)) {
setSaturation(savedState.getFloat(STATE_SATURATION));
} else {
setValue(savedState.getFloat(STATE_VALUE));
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (modifier instanceof Parcelable) {
outState.putParcelable(MODIFIER_KEY, (Parcelable)modifier);
} else {
throw new IllegalArgumentException(modifier + " must implement Parcelable!");
}
}
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.prevFirstVisiblePosition = mPrevFirstVisiblePosition;
ss.prevFirstVisibleChildHeight = mPrevFirstVisibleChildHeight;
ss.prevScrolledChildrenHeight = mPrevScrolledChildrenHeight;
ss.prevScrollY = mPrevScrollY;
ss.scrollY = mScrollY;
ss.childrenHeights = mChildrenHeights;
return ss;
}