Passing data between activity and fragments

Hi
I have installed Xamarin and using it with Visual Studio 2015 community.

My android-app uses ActionBar -menu and it will be created dynamically.
Actionbar consist of 4 fragments :

** ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;

        _fragments = new Fragment[]
                     {
                         new A(),
                         new B(),
                         new C(),
                         new D()
                     };

        AddTabToActionBar(Resource.String.a_tab_label, Resource.Drawable.a_png);
        AddTabToActionBar(Resource.String.b_tab_label, Resource.Drawable.b_png);

        AddTabToActionBar(Resource.String.c_tab_label, Resource.Drawable.c_png);
        AddTabToActionBar(Resource.String.d_tab_label, Resource.Drawable.d_png);**

I can open layout's using this code but..
For example : A-tab have button which should open new layout SearchName (popupwindows) and I should return data (Name) to A-tab.
And layout SearchName have also events which should be able to define
I've tried to find examples not found such of example which returns data from SearchName -activity my A -fragments.

Regards

mattini

Posts

  • lukasrein97lukasrein97 USMember ✭✭✭
    edited December 2016

    You can use SharedPreferences with the Preferences Manager.
    For Example you set a String with nothing in it and then in you SearchActivity with you Button you fill that string with a Name or something.
    And your TabActivity get the String from SharedPreferences after the string isnt "" or null.

    // THIS CODE IS FOR A ACTIVITY
    ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this); 
    ISharedPreferencesEditor editor = prefs.Edit();
    editor.PutString("yourReturnString", "yourReturnData"));
    editor.Apply();
    
    // THIS CODE IS FOR A FRAGMENT
    ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Application.Context); 
    ISharedPreferencesEditor editor = prefs.Edit();
    editor.PutString("yourReturnString", "yourReturnData"));
    editor.Apply();
    

    And in your TabActivity you can get the string like this:

    //THIS CODE IS FOR A ACTIVTY
    ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (this);
    String mString = prefs.GetString ("yourReturnString", "");
    
    //THIS CODE IS FOR A FRAGMENT
    ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (Application.Context);
    String mString = prefs.GetString ("yourReturnString", "");
    

    So you could use this Code in your TabActivity

    ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (Application.Context);
    String mString = prefs.GetString ("yourReturnString", "");
    
    if(!(mString == "" || mString == null))
    {
        //Do something with your Data you got from the SearchActivity
    } else
    {
        //Your Data isnt set do something
    }      
    
  • mattinimattini FIMember

    Thank's Lukas
    Now when I open SearchName -activity and find right name--> I use pref.PutString -method --> then I close activity using this.Finish() -method.
    Problem is how to update A-fragments . I should somehow fire an event/method inside fragment A from activity SearchName.

  • lukasrein97lukasrein97 USMember ✭✭✭

    I should somehow fire an event/method inside fragment A from activity SearchName.

    Yeah right. But i think in your A Fragment when your SearchName is finished the onResume() Methode of Fragment A starts? If thats wrong yeah then you have to fire a Event by yourselfe.

  • mattinimattini FIMember

    Thanks
    OnResume() solved problem.

  • lukasrein97lukasrein97 USMember ✭✭✭

    No Problem!
    Please mark my comment as answered so other people with the same problem can find the solution faster :smile:

Sign In or Register to comment.