Hello @Daniel Kelly ,
Welcome to our Microsoft Q&A platform!
the subscription is not removed which I am assuming because "this" is no longer the current instance of the app class
To avoid this, please call the unsubscribe command before the app is closed. Try creating a global static
instance of the App
class so that we can get the App instance to execute the unsubscribe method in other classes.
public partial class App : Application
{
public static App Instance;
public App()
{
InitializeComponent();
Instance = this;
MainPage = new NavigationPage(new TestPage());
MessagingCenter.Subscribe<TestPage>(this, "hi", (args) => {
//
});
}
}
Then detect the application finished lifecycle method in the native platform. For example, detect the OnDestory
method on Android.
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnDestroy()
{
base.OnDestroy();
MessagingCenter.Unsubscribe<TestPage>(App.Instance, "hi");
}
}
Best Regards,
Jarvan Zhang
If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.