From 7e9be32e28ed96c2dd6f232c16f149222b7985d6 Mon Sep 17 00:00:00 2001 From: farfromrefuge Date: Wed, 3 Jul 2024 21:23:01 +0200 Subject: [PATCH] fix(android): prevent error while opening modal from background (#10570) It is a known android "issue" that you cant commit/show a fragment while in background. The reason is, as explained [here](https://medium.com/@113408/avoid-fragment-illegalstateexception-can-not-perform-this-action-after-onsaveinstancestate-ba76ae4f00fe) or [here](https://stackoverflow.com/questions/15729138/on-showing-dialog-i-get-can-not-perform-this-action-after-onsaveinstancestate), that `onSaveInstanceState` is already called so any operation before activity start would be with state loss. There are 2 solutions in this case: * use `commitAllowingStateLoss`, `dismissAllowingStateLoss` ... but then you loose state ... This is what we are doing in N in many cases. We can do this with `show` too but we would need to override the `DialogFragment.show` method. * delay the action until the activity is resumed. This PR uses the second solution. We could add an option to `showModal` to use the first solution. The user experience is different. Solution 1: when the app is resumed the modal is already shown and layed out. Solution 2: you see the modal opening on app resume --- packages/core/ui/core/view/index.android.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/core/ui/core/view/index.android.ts b/packages/core/ui/core/view/index.android.ts index 5101f8a4c..e6d1e2fd0 100644 --- a/packages/core/ui/core/view/index.android.ts +++ b/packages/core/ui/core/view/index.android.ts @@ -684,6 +684,17 @@ export class View extends ViewCommon { return result | (childMeasuredState & layout.MEASURED_STATE_MASK); } protected _showNativeModalView(parent: View, options: ShowModalOptions) { + // if the app is in background while triggering _showNativeModalView + // then DialogFragment.show will trigger IllegalStateException: Can not perform this action after onSaveInstanceState + // so if in background we create an event to call _showNativeModalView when loaded (going back in foreground) + if (Application.inBackground && !parent.isLoaded) { + const onLoaded = ()=> { + parent.off('loaded', onLoaded) + this._showNativeModalView(parent, options); + }; + parent.on('loaded', onLoaded); + return; + } super._showNativeModalView(parent, options); initializeDialogFragment();