Forum Xamarin Xamarin.Android

communicating with fragment from activity on back press

aaravaarav INMember ✭✭

hi all and this my second thread and i hope i get your kind attention

i have a tabbed app with a view pager and in first tab or fragement on a text view click i am going to a new activity where i have list view now what i want is when i click on the list view it should take the data and come back to first tab and show there so i want some help how communicate data from an activity to fragment ....

Answers

  • jaymarvelsjaymarvels GBMember ✭✭

    This should be possible.

    You create the method on your new activity that you want to consume the data. For example:

    public class MyNewActivity { public void DisplayData(List<string> MyData) { // Do stuff } }

    Then in your fragment you can call this:

    public class MainPlayerListFragment : Fragment
    {
        public void SendDataToActivity()
        {
            ((MyNewActivity)Activity).DisplayData(myData);
        }    
    }
    
  • aaravaarav INMember ✭✭

    @jaymarvels ((MyNewActivity)Activity).DisplayData >>(myData);<<
    what is my data here can u please explain

  • jaymarvelsjaymarvels GBMember ✭✭

    @aarav whatever data you want to send to your Activity. In the above example the method the fragment is calling expects a List but you can obviously change this to whatever you want

  • aaravaarav INMember ✭✭

    @jaymarvels i think i got my data by another way but now i have another doubt what i did is on ListView_ItemClick i am sending the data to the activity in which fragment 1 is attached now how can i send the data from fragment activity to fragment 1

  • jaymarvelsjaymarvels GBMember ✭✭

    @aarav Sorry, I don't understand what you mean

  • aaravaarav INMember ✭✭

    @jaymarvels sorry for sounding like a noob what my app does is i have 2 tab fragments which are swipable i have done it via view pager now in tab1 there is a textview when some clicks on text view it redirect to a new activity let us call new activity as ACTIVITY B with a new layout which consists of a list view when some one clicks on the list item it takes back to the 1 st tab shows the data in the text view but i cant show the data in text view i cant pass it now what i have done is i have sent data from ACTIVITY B to ACTIVITY A . Activity A is the activity which embed for both the fragment now what i want is to send data from embeded Activity A to tab 1 or fragment 1 or how can i send data from ACTIVITY B to Fragment A or tab 1 if u need my code i can attach it too.... thanks @jaymarvels foe your time

  • jaymarvelsjaymarvels GBMember ✭✭

    @aarav

    Probably best share some code. Pop it onto Gist or something

  • aaravaarav INMember ✭✭

    @jaymarvels and sorry for being late

Sign In or Register to comment.