我是Angular开发人员,在过去的几个月中一直在尝试Nativescript。到目前为止,一切都很好。
在我正在使用的应用程序上,我遇到一个问题,即从启动器重新打开应用程序后,UI(android活动)会重新启动,但背景仍在工作(这是我希望从背景)。
我已经看到这是android及其可解决的问题。我确实尝试用nativescript复制解决方案,但无法完成。我进行了自定义活动,但在从午餐者重新打开应用程序后遇到了以下错误。
> An uncaught Exception occurred on "main" thread. Unable to destroy
> activity {org.nativescript.Challanger/org.challenger.MainActivity}:
> com.tns.NativeScriptException: Calling js method onDestroy failed
> Error: View not added to this instance. View: t(2) CurrentParent:
> undefined ExpectedParent: t(1)
StackTrace:
java.lang.RuntimeException: Unable to destroy activity {org.nativescript.Challanger/org.challenger.MainActivity}: com.tns.NativeScriptException: Calling js method onDestroy failed
Error: View not added to this instance. View: t(2) CurrentParent: undefined ExpectedParent: t(1)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3945)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3963)
at android.app.ActivityThread.access$1700(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1454)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
Caused by: com.tns.NativeScriptException: Calling js method onDestroy failed
Error: View not added to this instance. View: t(2) CurrentParent: undefined ExpectedParent: t(1)
at com.tns.Runtime.callJSMethodNative(Native Method)
at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1242)
at com.tns.Runtime.callJSMethodImpl(Runtime.java:1122)
at com.tns.Runtime.callJSMethod(Runtime.java:1109)
at com.tns.Runtime.callJSMethod(Runtime.java:1089)
at com.tns.Runtime.callJSMethod(Runtime.java:1081)
at org.challenger.MainActivity.onDestroy(MainActivity.java:34)
at android.app.Activity.performDestroy(Activity.java:6526)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1143)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3932)
... 9 more
我确实尝试过使用android:launchMode="singleTask" and "singleInstance"
,但他们没有帮助解决该问题。
这是我使用的主要活动的一部分。
public onCreate(savedInstanceState: any): void { // android.os.Bundle
// Set isNativeScriptActivity in onCreate (as done in the original NativeScript activity code).
// The JS constructor might not be called because the activity is created from Android.
this.isNativeScriptActivity = true;
if (!this._callbacks) {
setActivityCallbacks(this);
}
this._callbacks.onCreate(this, savedInstanceState, this.getIntent(), super.onCreate);
if (!this.isTaskRoot()) {
let intent: android.content.Intent = this.getIntent();
let action: string = intent.getAction();
if (intent.hasCategory(android.content.Intent.CATEGORY_LAUNCHER) && action === android.content.Intent.ACTION_MAIN) {
this.finish();
return;
}
}
}
public onDestroy(): void {
this._callbacks.onDestroy(this, super.onDestroy);
}
如何防止活动重新启动?我感觉自己很近,但是我缺少一些东西。