Reputation: 21
I try to handle the android lifecycle in my NativeScript android app using the following guide: https://docs.nativescript.org/core-concepts/application-lifecycle#android-activity-events
When i use the back button to quit the app and then the recent button to reopen the app, all the lifecycle events are triggered twice. If i do ti again, all the lifecycle events are triggered thrice.
Here is a playground simple app that shows the problem: https://play.nativescript.org/?template=play-ng&id=y9RucD
Use the back button and then the recent button to resume...
Upvotes: 0
Views: 805
Reputation: 4574
You need to remove listeners on destroy event. As you are using android.on
to assign the event listeners, you need to use the android.off
as well.
You can find the a complete example here and here. I have also updated the your playground.
In your ngOnInit function I am assigning android.on to a listener e.g.
this.launchListenerCB = (args) => {
console.log(">>>>>>> resumeEvent Event");
if (args.android) {
// For Android applications, args.android is an android.content.Intent class.
console.log("resumeEvent Android application with the following intent: " + args.android + ".");
}
};
appOn(resumeEvent, this.launchListenerCB);
and on exitEvent I am unsubscribing to all listeners.
this.exitListenerCB = (eventData: any) => {
this.unsubscribeAll();
}
appOn(exitEvent, this.exitListenerCB);
private unsubscribeAll(): void {
// console.log("unsubscribeAll launchListenerCB:", !!launchListenerCB)
appOff(resumeEvent, this.launchListenerCB); // HERE
// appOff(suspendEvent, this.suspendListenerCB);
// appOff(resumeEvent, this.resumeListenerCB);
// appOff(lowMemoryEvent, this.lowMemoryListenerCB);
// appOff(exitEvent, this.exitListenerCB);
}
In your playground, I have just used ResumeEvent to show you the code, you can assign/unassign to other events as well.
Upvotes: 1