In this Xamarin Activity a MessagingCenter Subscribe is done during OnCreate and Unsubscribe during OnDestroy. When the app loads it Subscribes and when pressing back button OnDestroy is called after OnBackPressed. So the cycle seems correct. However, every time back is pressed and the app is started again the registered function retrieveSorterScan is called an extra time. After 10 times restarting it is called 10 times even though unsubscribe / subscribe is called. I think it boils down to the following combination of functions. What could be wrong here?
edit after removing OnBackPressed it has the same behaviour so thats not the issue.
[Activity(Label = "RopsSorterApp", Icon = "@drawable/icon", Theme = "@style/MainTheme", LaunchMode = LaunchMode.SingleTask, MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Landscape)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity {
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Forms.Init(this, bundle);
Instance = this;
app = new App();
LoadApplication(app);
MessagingCenter.Instance.Subscribe<Forms.Application, SorterScan>(Forms.Application.Current, INFO, app.MainPage.retrieveSorterScan);
}
protected override void OnDestroy()
{
base.OnDestroy();
MessagingCenter.Instance.Unsubscribe<Forms.Application, string>(Forms.Application.Current, INFO);
}
question from:
https://stackoverflow.com/questions/65933018/xamarin-application-lifecycle-with-messagingcenter 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…