I have the following in my main-view:
- TabView with 5 TabViewItems
- The first tab (Tab1) has a ScrollView and a listener on the scroll event.
- The scroll event is attached inside the loaded event of the ScrollView
All works as long as you stay inside the app. The problem occurs when you switch between apps or homescreen (background / foreground / activity resume).
- If the user is on Tab1 or Tab2, sends app to background, then resumes = Everything works fine.
- If the user is on Tab3 or higher, sends app to background, then resumes = crash & burn.
This is probably due to the fact that Android uses ViewPager as TabView, since it recycles views.
I'm just not sure how to deal with events when the view gets recycled. I tried also removing the scroll event in the unloaded event of the ScrollView. It worked almost, it didn't crash on resume, but it did as soon as I went back to the first view and started scrolling. Also the unloaded event didn't fire always...
I have attached a demo-app that shows the issue. But you do have to do as described above. Move to Tab3, exit and resume.
An uncaught Exception occurred on "main" thread.
java.lang.RuntimeException: Unable to resume activity {org.nativescript.tabdemo/com.tns.NativeScriptActivity}: com.tns.NativeScriptException:
Calling js method onStart failed
TypeError: Cannot read property 'getViewTreeObserver' of undefined
File: "file:///data/data/org.nativescript.tabdemo/files/app/tns_modules/tns-core-modules/ui/scroll-view/scroll-view.js, line: 125, column: 21
StackTrace:
Frame: function:'ScrollView.attachNative', file:'file:///data/data/org.nativescript.tabdemo/files/app/tns_modules/tns-core-modules/ui/scroll-view/scroll-view.js', line: 125, column: 22
Frame: function:'ScrollView.attach', file:'file:///data/data/org.nativescript.tabdemo/files/app/tns_modules/tns-core-modules/ui/scroll-view/scroll-view-common.js', line: 49, column: 18
Frame: function:'ScrollView.onLoaded', file:'file:///data/data/org.nativescript.tabdemo/files/app/tns_modules/tns-core-modules/ui/scroll-view/scroll-view-common.js', line: 41, column: 14
...
at com.tns.Runtime.callJSMethodNative(Native Method)
JS: at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1197)
JS: at com.tns.Runtime.callJSMethodImpl(Runtime.java:1061)
JS: at com.tns.Runtime.callJSMethod(Runtime.java:1047)
JS: at com.tns.Runtime.callJSMethod(Runtime.java:1028)
JS: at com.tns.Runtime.callJSMethod(Runtime.java:1018)
JS: at com.tns.NativeScriptActivity.onStart(android.app.Activity.java)
JS: at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
JS: at android.app.Activity.performStart(Activity.java:6253)
JS: at android.app.Activity.performRestart(Activity.java:6299)
JS: at android.app.Activity.performResume(Activity.java:6304)
JS: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
JS: ... 8 more
Demo-app:
https://www.dropbox.com/s/qf04ny6y0ugura1/TabScrollCrash.zip?dl=0
My setup:
- NativeScript: 2.5.2
- Core-Modules: 2.5.2
- Android-runtime: 2.5.0
- Android API: 25
I have the following in my main-view:
All works as long as you stay inside the app. The problem occurs when you switch between apps or homescreen (background / foreground / activity resume).
This is probably due to the fact that Android uses ViewPager as TabView, since it recycles views.
I'm just not sure how to deal with events when the view gets recycled. I tried also removing the scroll event in the unloaded event of the ScrollView. It worked almost, it didn't crash on resume, but it did as soon as I went back to the first view and started scrolling. Also the unloaded event didn't fire always...
I have attached a demo-app that shows the issue. But you do have to do as described above. Move to Tab3, exit and resume.
An uncaught Exception occurred on "main" thread. java.lang.RuntimeException: Unable to resume activity {org.nativescript.tabdemo/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onStart failed TypeError: Cannot read property 'getViewTreeObserver' of undefined File: "file:///data/data/org.nativescript.tabdemo/files/app/tns_modules/tns-core-modules/ui/scroll-view/scroll-view.js, line: 125, column: 21 StackTrace: Frame: function:'ScrollView.attachNative', file:'file:///data/data/org.nativescript.tabdemo/files/app/tns_modules/tns-core-modules/ui/scroll-view/scroll-view.js', line: 125, column: 22 Frame: function:'ScrollView.attach', file:'file:///data/data/org.nativescript.tabdemo/files/app/tns_modules/tns-core-modules/ui/scroll-view/scroll-view-common.js', line: 49, column: 18 Frame: function:'ScrollView.onLoaded', file:'file:///data/data/org.nativescript.tabdemo/files/app/tns_modules/tns-core-modules/ui/scroll-view/scroll-view-common.js', line: 41, column: 14 ... at com.tns.Runtime.callJSMethodNative(Native Method) JS: at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1197) JS: at com.tns.Runtime.callJSMethodImpl(Runtime.java:1061) JS: at com.tns.Runtime.callJSMethod(Runtime.java:1047) JS: at com.tns.Runtime.callJSMethod(Runtime.java:1028) JS: at com.tns.Runtime.callJSMethod(Runtime.java:1018) JS: at com.tns.NativeScriptActivity.onStart(android.app.Activity.java) JS: at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237) JS: at android.app.Activity.performStart(Activity.java:6253) JS: at android.app.Activity.performRestart(Activity.java:6299) JS: at android.app.Activity.performResume(Activity.java:6304) JS: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092) JS: ... 8 moreDemo-app:
https://www.dropbox.com/s/qf04ny6y0ugura1/TabScrollCrash.zip?dl=0
My setup: