Hello,
Welcome to our Microsoft Q&A platform!
Yes, it is necessary to unsubscribe from a message when a class no longer wishes to receive the message.
This does not happen if the User just hits the Home button and comes back to the app or terminates the app by swiping it off the Apps list.
I have noticed this also happens when the Battery Saver mode is On and Android kills the app when its sent to the background.
You can try to detect your app being killed. You can use service to do this.
Please refer to the following code:
[Service]
public class OnClearFromRecentService : Service
{
public override IBinder OnBind(Intent intent)
{
return null;
}
[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
return base.OnStartCommand(intent, flags, startId);
}
public override void OnDestroy()
{
base.OnDestroy();
}
public override void OnTaskRemoved(Intent rootIntent)
{
base.OnTaskRemoved(rootIntent);
// add your code
}
}
Note: And now whenever you clear your app from android recent ,then method onTaskRemoved()
will execute.
In addition,the MessagingCenter helps you keep your code decoupled. Sometimes you will find yourself in a position that requires you create a reference between certain code, but by doing so, you have to compromise on reusability and maintainability.
So try to use it as a last resort, and usually there should be another way to achieve your desired functionality. While sending a message can be very powerful, using it too much can really eat into your readability.
Best Regards,
Jessie Zhang
---
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.