NativeScript icon indicating copy to clipboard operation
NativeScript copied to clipboard

Error: Calling js method onCreateView failed; Expected the adapter to be 'fresh' while restoring state.

Open dlcole opened this issue 4 years ago • 10 comments

Issue Description

On Android, if my app navigates away from the app (such as opening a Google map) and then returns, returning to the prior page via the back button causes this error:

JS: app.js uncaught error: Error: Calling js method onCreateView failed
JS: Error: java.lang.IllegalStateException: Expected the adapter to be 'fresh' while restoring state.
ActivityManager: Process tech.govia.rrr (pid 22075) has died: fore TOP (326,1134)
ActivityManager: setHasOverlayUi called on unknown pid: 22075

I was able to identify the problem in @nativescript/core/ui/frame/index.android.js starting at line 755:

 const savedState = entry.viewSavedState;
        if (savedState) { 
            page.nativeViewProtected.restoreHierarchyState(savedState);
            entry.viewSavedState = null;
        }

The problem is that savedState in this case is an empty object, but tests for true. I replaced line 756 with

if (savedState && Object.keys(savedState).length > 0) {

and this is now working correctly.

Reproduction

Mine is a Nativecript JavaScript project; this problem likely applies to other project types as well. On a secondary page within your app, open a window that causes the app to be suspended, such as opening a Contacts selection window or a Google map. Upon returning to your app, tap the back button so as to return to the prior page. The error shown above will occur. This problem only occurs on Android.

Relevant log output (if applicable)

No response

Environment

OS: macOS 12.2.1
CPU: (8) x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
Shell: /bin/bash
node: 16.13.0
npm: 8.1.0
nativescript: 8.1.5

# android
java: 11.0.13
ndk: Not Found
apis: Not Found
build_tools: Not Found
system_images: Not Found

# ios
xcode: 13.3/13E113
cocoapods: 1.8.4
python: 2.7.18
python3: 3.9.5
ruby: 2.6.8
platforms: 
  - DriverKit 21.4
  - iOS 15.4
  - macOS 12.3
  - tvOS 15.4
  - watchOS 8.5

Dependencies

"dependencies": {
  "@bradmartin/nativescript-urlhandler": "^2.0.1",
  "@kefah/nativescript-google-maps": "^1.0.7",
  "@master.technology/permissions": "^2.0.1",
  "@nativescript-community/ui-material-bottom-navigation": "^7.0.2",
  "@nativescript/appversion": "^2.0.0",
  "@nativescript/contacts": "^2.0.4",
  "@nativescript/core": "^8.2.1",
  "@nativescript/email": "^2.0.5",
  "@nativescript/firebase": "^11.1.3",
  "@nativescript/geolocation": "^8.0.2",
  "@nativescript/iqkeyboardmanager": "^2.0.0",
  "@nativescript/theme": "^3.0.2",
  "@triniwiz/nativescript-toasty": "^4.1.3",
  "base-64": "^1.0.0",
  "nativescript-bitmap-factory": "^1.8.1",
  "nativescript-clipboard": "^2.1.1",
  "nativescript-danem-google-maps-utils": "^1.0.18",
  "nativescript-drop-down": "^6.0.1",
  "nativescript-pdf-view": "^3.0.0-1",
  "nativescript-phone": "^3.0.3",
  "nativescript-screenshot": "^0.0.2",
  "nativescript-sqlite": "^2.8.6",
  "nativescript-ui-listview": "^10.0.2",
  "nativescript-ui-sidedrawer": "^10.0.2",
  "patch-package": "^6.4.7"
},
"devDependencies": {
  "@nativescript/android": "8.1.1",
  "@nativescript/ios": "8.2.3",
  "@nativescript/webpack": "^5.0.6"
}

Please accept these terms

dlcole avatar Apr 05 '22 16:04 dlcole

Exactly the same for me.

romandrahan avatar Apr 12 '22 01:04 romandrahan

@dlcole actually it is not an empty object. It is(should be) a native object. Could you try and console.log things like savedState.constructor.name, typeof savedState or even Object.keys(savedState) ?

farfromrefug avatar Apr 12 '22 08:04 farfromrefug

@farfromrefug I added this line:

const savedState = entry.viewSavedState;
  if (savedState)
    console.log("@nativescript/core/ui/frame/index.android.js: Object.keys(savedState).length: " + Object.keys(savedState).length );

and then when I run the scenario above, I get

@nativescript/core/ui/frame/index.android.js: Object.keys(savedState).length: 0

It may be that the real problem is upstream where the saved state is created, but my patch at least kept me running.

And, thanks for the reply. I've used NativeScript for over 5 years but I'm really struggling with migrating my project from 6.x to 8.x. My two current stop-ship issues are long start-up delay on iOS and structural problem in the manifest on Android 12.

dlcole avatar Apr 12 '22 13:04 dlcole

@dlcole Same problem happening with the following stack trace: System.err: Calling js method finishUpdate failed System.err: Error: java.lang.IllegalStateException: Expected the adapter to be 'fresh' while restoring state. System.err: System.err: StackTrace: System.err: FragmentPagerAdapter._commitCurrentTransaction(file: src\webpack:\salesmobilecloud\node_modules@nativescript\core\ui\tab-view\index.android.js:212:0) System.err: at FragmentPagerAdapter.finishUpdate(file: src\webpack:\vendasmobilecloud\node_modules@nativescript\core\ui\tab-view\index.android.js:192:0)

juniorschen avatar Apr 27 '22 22:04 juniorschen

@dlcole I am experiencing the exact same behavior, expect i only experience the issue when navigating back to a page that contains a tabview, more specifically the community tab plugin https://www.npmjs.com/package/@nativescript-community/ui-material-tabs

the change that you suggested in the description solves the issue that im experiencing

@farfromrefug Not sure if this is still relevant, but the output i get from the console logs you requested is as follows:

@@@@@@@@@@@@@@@
savedState?.constructor?.name: 
typeof savedState: object
Object.keys(savedState): []
@@@@@@@@@@@@@@@

J-LunZA avatar May 10 '22 12:05 J-LunZA

@dlcole ok so i cant reproduce the issue but i might understand what s going on. You were right on though i think there is a better way to write it. this i think should be replaced with:

if (viewState.size() > 0) {
     entry.viewSavedState = viewState;
}

@dlcole can you try this fix?

farfromrefug avatar Jun 13 '22 14:06 farfromrefug

@farfromrefug I made a demo-app reproducing the issue. At first I was not able to recreate it, as I thought it was caused by the changes in the styles.xml -> parent="Theme.MaterialComponents.Light.NoActionBar">.

But it seems it is related to @nativescript-community/ui-material-bottom-navigation. Once I added this to the main view, the crash was reproducable.

Open app, click on a item in the list > navigate to subview > go to home screen > go back to the app > tap Back = crash

android_frame_crash_demo.zip

manijak avatar Jun 15 '22 14:06 manijak

@manijak thanks a lot for the example project! Thanks to it i managed to fix it the right way! It was an issue with BottomNavigation indeed. It is fixed in@nativescript-community/ui-material-bottom-navigation 7.0.20

farfromrefug avatar Jun 16 '22 15:06 farfromrefug

Awesome, glad we nailed it. 👍

manijak avatar Jun 16 '22 15:06 manijak

I won't have a chance to test the fix for a couple weeks yet, but I'm glad it was fixed at the source!

dlcole avatar Jun 17 '22 17:06 dlcole