diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java index a6884db857a..45365d285a2 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java @@ -9,6 +9,7 @@ import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Looper; +import android.os.Parcel; import android.text.TextUtils; import android.view.DragEvent; import android.view.Menu; @@ -51,6 +52,7 @@ import com.google.android.material.snackbar.Snackbar; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; +import org.jetbrains.annotations.NotNull; import org.wordpress.android.BuildConfig; import org.wordpress.android.R; import org.wordpress.android.WordPress; @@ -1156,9 +1158,29 @@ public class EditPostActivity extends LocaleAwareActivity implements } } + private Boolean isTooLargeBundle(@NotNull Bundle bundle) { + Parcel parcel = Parcel.obtain(); + int size; + parcel.writeBundle(bundle); + size = parcel.dataSize(); + int maximumParcelSize = 10 * 1024; // limit to 500 KB + parcel.recycle(); + AppLog.i(T.EDITOR, "Bundle size: " + size + " bytes"); + return size > maximumParcelSize; + } + @Override - protected void onSaveInstanceState(Bundle outState) { + protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); + if (isTooLargeBundle(outState)) { + // Don't save view state when the instance state is too large + // to avoid when the state is too large to avoid a TransactionTooLargeException + // see: https://github.com/wordpress-mobile/WordPress-Android/issues/9685 + // and: https://github.com/facebook/react-native/issues/19458 + outState.remove("android:viewHierarchyState"); + outState.remove("androidx.lifecycle.BundlableSavedStateRegistry.key"); + AppLog.e(T.EDITOR, "Dumping view hierarchy state to avoid TransactionTooLargeException"); + } // Saves both post objects so we can restore them in onCreate() updateAndSavePostAsync(); outState.putInt(STATE_KEY_POST_LOCAL_ID, mEditPostRepository.getId());