0

I am trying to get data from fragmentA into my other fragment i.e fragmentB. In my fragmentA there is a Edittext field and when user hit submit button, the data from Edittext should display in my fragmentB. I tried to follow this link but since I am new to xamarin mobile dev area I couldn't figure out the answer. Can anyone help me with this issue.

I tried doing this but I am not getting any data from fragmentA to fragmentB
fragmentA

submitButton.Click += delegate
        {
            TrackInfoFragment fragment = new TrackInfoFragment();
            Bundle bundle = new Bundle();
            bundle.PutString("message", "From Activity");
            fragment.Arguments = bundle;
        };

fragmentB

Bundle bundle = new Bundle();
        string test = bundle.GetString("message");
        Console.WriteLine("Test: " + test);
Nick King
  • 190
  • 3
  • 20
  • If you use Xamarin Forms, then the easy way to pass data between two components is to use [MessagingCenter](https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/messaging-center/). However, I suspect you use Xamarin Android. In that case you can take a look at [MvvmCross](https://github.com/MvvmCross/MvvmCross). It's a cross platform library that encourages Mvvm pattern and allows you to move even more application code into a cross platform library. As a bonus it also contains a messaging (Publisher/Subscriber) component. – Anatoliy Pidlubnyy Jun 19 '17 at 17:39
  • @AnatoliyPidlubnyy in my separate tutorial project, I tried using MvvmCross framework but it had the same issue and also there are not many tutorials out there for api integration, communicating between components, etc. and all this things I need into my application. That's why I am not using this framework. If you have any good tutorial for this framework then please let me know. I can give a go again. Thanks – Nick King Jun 19 '17 at 17:52
  • I feel your pain. I took me a bit of googling to convert my Xamarin.Androind app MvvmCross. The documentation is a bit lacking but the framework is worth of time spending to learn it. Here is a [link](https://stackoverflow.com/a/15592431/6105337) to help you figure out how to use the MvvmCross Messenger. – Anatoliy Pidlubnyy Jun 19 '17 at 19:45
  • And the link to the [Messenger plugin documentation](https://www.mvvmcross.com/documentation/plugins/messenger?scroll=1238) – Anatoliy Pidlubnyy Jun 19 '17 at 19:48

1 Answers1

0

You are instantiating a new bundle. On fragmentB, get data from Arguments.

Instead of:

Bundle bundle = new Bundle();
string test = bundle.GetString("message");
Console.WriteLine("Test: " + test);

Try

string test = Arguments.GetString("message");
Console.WriteLine("Test: " + test);

Hope it helps...

Sources: Xamarin Android passing variable from Activity to Fragment returns null

leosf6308
  • 109
  • 6
  • I'm sorry, but can you try this (from https://forums.xamarin.com/discussion/comment/70756/#Comment_70756) on the calling Activity: Bundle bundle = new Bundle(); bundle.PutString("message", "From Activity"); TrackInfoFragment fragment = new TrackInfoFragment() { Arguments = bundle } As the answer says, arguments needs to get persisted... – leosf6308 Jun 19 '17 at 18:05