Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 48981

How to perform cleanup task when app is force closed by user

$
0
0

I need to perform cleanup task when app is force closed by user (by swiping up). The scenario is:
I am using MusicControls plugin to display controls in the notification area. When the user closes the app by swiping, I want to destroy the Music Controls from the notification and lock screen. Currently, the musci controls remain in the lock screen area even after the app is closed.
Currently I am trying for android with Ionic 5 & Angular.
Here are few of the things I have tried:

  1. Called the cleanup code (which destroys the Music Controls) in ngOnDestory in app.component.ts. Does not work.
  2. Registered window listener of beforeunload/ unload within this.plaform.ready and called cleanup code within the listener
  3. Used @HostListener as suggested in some sites.

Here are the code snippets from my attempts:
Attempt 1:

ngOnDestroy() { 
    this.cleanUp();
}

Attempt 2:

this.platform.ready().then(() => {
      window.addEventListener('beforeunload', () => {
        // is called when app is forced and closed
        console.log('Before unload is called, destroy controls here');
        this.cleanUp();
      });
}

Attempt 3:

@HostListener('window:beforeunload', ['$event'])
beforeUnloadHander(event) {
 this.cleanUp();
    return false;
}

I have tried attempt 2 and attempt 3 with unload as well as beforeunload.
None of these work for me. Is there anything that I am doing wrong? This is very important, since the music controls is just hanging there and it can’t be dismissed by the user either.

Here is my musiccontrols configuration in case it is useful:

this.musicControls.create({
      track: title,
      artist: artist,
      isPlaying: true,
      hasPrev: hasPrev,
      hasNext: hasNext,
      hasClose: true,
      dismissable: true,

      playIcon: 'media_play',
      pauseIcon: 'media_pause',
      prevIcon: 'media_prev',
      nextIcon: 'media_next',
      closeIcon: 'media_close',
      notificationIcon: 'notification'
    });

Any help appreciated.
Thank you in advance.
This is my first post here, I hope I have followed the protocols correctly.

3 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 48981

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>