Content-Length: 283586 | pFad | http://github.com/NativeScript/NativeScript/commit/7e9be32e28ed96c2dd6f232c16f149222b7985d6

BD fix(android): prevent error while opening modal from background (#10570) · NativeScript/NativeScript@7e9be32 · GitHub
Skip to content

Commit 7e9be32

Browse files
authored
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
1 parent 80f3ff2 commit 7e9be32

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

packages/core/ui/core/view/index.android.ts

+11
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,17 @@ export class View extends ViewCommon {
684684
return result | (childMeasuredState & layout.MEASURED_STATE_MASK);
685685
}
686686
protected _showNativeModalView(parent: View, options: ShowModalOptions) {
687+
// if the app is in background while triggering _showNativeModalView
688+
// then DialogFragment.show will trigger IllegalStateException: Can not perform this action after onSaveInstanceState
689+
// so if in background we create an event to call _showNativeModalView when loaded (going back in foreground)
690+
if (Application.inBackground && !parent.isLoaded) {
691+
const onLoaded = ()=> {
692+
parent.off('loaded', onLoaded)
693+
this._showNativeModalView(parent, options);
694+
};
695+
parent.on('loaded', onLoaded);
696+
return;
697+
}
687698
super._showNativeModalView(parent, options);
688699
initializeDialogFragment();
689700

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/NativeScript/NativeScript/commit/7e9be32e28ed96c2dd6f232c16f149222b7985d6

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy