41850

Getting Messege Twice Using IMvxMessenger

I am working on Xamrin Android Application and I am using MvvmCross.In my application I have used IMvxMessenger.But I am getting messsge twice when I change the view and comeback to same view.And then It is Crashed. What is the solution ? Here is my code : In ViewModel I have this code :

var messanger = Mvx.Resolve<IMvxMessenger>(); var message = new MyMessage(this,"Product has been added",cartListCount); messanger.Publish(message);

And On View I have this code:

private MvxSubscriptionToken _token; public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { var view = base.OnCreateView(inflater, container, savedInstanceState); messenger = Mvx.Resolve<IMvxMessenger>(); _token = messenger.SubscribeOnMainThread<MyMessage>(OnInputIsNeeded,MvxReference.Strong); return view; } private void OnInputIsNeeded(MyMessage _Message) { Toast.MakeText (this.Activity,_Message.Message, ToastLength.Short).Show(); messenger.Unsubscribe<MyMessage> (_token); }

Answer1:

Views (whether Activities or Fragments) are created and destroyed all the time. Since you are subscribing in OnCreateView(), you should unsubscribe in OnDestroyView().

What's likely happening is that OnCreateView() is being called multiple times and causing multiple subscriptions to be added. MvxMessenger is broadcasting the message to all subscribers. If a View is destroyed, but hasn't unsubscribed, it's likely to crash since the callback no longer exists.

Recommend

  • System.FormatException in MVVMCross
  • Azure eventhub multiple partition key points to same partition
  • spring integration time out clients
  • message box in asp.net web application
  • Android : Alarm Notification Issue
  • Combine two or more arrays with differnet formant in php
  • Grails 3 - How to publish to Artifactory
  • Why is my SqlPackage DeployReport returning an empty report?
  • Apply a gradle plugin with a common configuration to all projects
  • Mocking Non-Standard Events in F# Foq
  • Retrieve list of sent friend requests from friend_request FQL table
  • NHibernate Validation Localization with S#arp Architecture
  • Using variable in a value field in jMeter
  • Adding a button at the bottom of a table view
  • How can I send an e-mail from a vbs script
  • Deleting and Updating values from a cusrsor adapter
  • Accessing IRQ description array within a module and displaying action names
  • Is possible to count alias result on mysql
  • output of program is not same as passed argument
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Javascript + PHP Encryption with pidCrypt
  • Importing jscolor library in angular 2
  • To display the title for the current loaction in map in iphone
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • php design question - will a Helper help here?
  • Cannot Parse HTML Data Using Android / JSOUP
  • KeystoneJS: Relationships in Admin UI not updating
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • How to load view controller without button in storyboard?