Home > Back-end >  send data from a fragment to an activity xamarin android
send data from a fragment to an activity xamarin android

Time:05-20

I'm sorry I'm using google translate.

I am trying to send data from a Fragment to an Activity with no success, the purpose of this is to update a TextView. I can't validate that the Intent brings the data and if I eliminate the validation they have a null value, I detail my code.

Fragment

private void sendData()
 {
    Intent i = new Intent(Context,Activity.Class);
    //PACK DATA
    i.PutExtra("COUNT", "1");
    //START ACTIVITY
    Activity.StartActivity(i);
}

Invoked

void ButtonClicked2(object sender, EventArgs args)
    {
        sendData();
    }

Activity

protected override void OnResume()
    {
        base.OnResume();
        //Intent i = Intent;
        Bundle sender = Intent.Extras;

        ////IF ITS THE FRAGMENT THEN RECEIVE DATA
        if (sender != null)
        {
            receiveData();
            Android.Widget.Toast.MakeText(this, "Received", Android.Widget.ToastLength.Short).Show();

        }
       
    }
    private void receiveData()
    {
        //RECEIVE DATA VIA INTENT
        Intent i =  Intent;
       string count = i.GetStringExtra("COUNT");
       
        //SET DATA TO TEXTVIEWS
        Android.Widget.TextView CountView = FindViewById<Android.Widget.TextView>(Resource.Id.itemCount);
        CountView.SetText(count, Android.Widget.TextView.BufferType.Normal);
        Android.Widget.Toast.MakeText(this, count, Android.Widget.ToastLength.Short).Show();
    }

CodePudding user response:

A simple example about how to pass data from fragement to Activity for your reference.

Fragement:

MyFragement:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:id="@ id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
<Button
    android:id="@ id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>

MyFragement.cs:

public class MyFragment : Fragment
{
     
    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

    
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        // return inflater.Inflate(Resource.Layout.YourFragment, container, false);

        // A large amount of text to display.
        var textToDisplay = "Hello";

        var view = inflater.Inflate(Resource.Layout.MyFragment, container, false);

        var textView = view.FindViewById<TextView>(Resource.Id.textView1);
        textView.Text = textToDisplay;

        var button = view.FindViewById<Button>(Resource.Id.button1);
        button.Click  = Button_Click;           

        return view;
    }

    private void Button_Click(object sender, EventArgs e)
    {
        sendData();
    }
    private void sendData()
    {
        Intent i = new Intent(Context, Activity.Class);
        //PACK DATA
        i.PutExtra("COUNT", "1");
        //START ACTIVITY
        Activity.StartActivity(i);
    }
}

Activity:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@ id/fragment_container"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>

MainAcitivity.cs:

  public class MainActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
    
        var newFragment = new MyFragment();
        var ft = FragmentManager.BeginTransaction();
        ft.Add(Resource.Id.fragment_container, newFragment);
        ft.Commit();

    }
    protected override void OnResume()
    {
        base.OnResume();
        //Intent i = Intent;
        Bundle sender = Intent.Extras;

        ////IF ITS THE FRAGMENT THEN RECEIVE DATA
        if (sender != null)
        {
            receiveData();
            Android.Widget.Toast.MakeText(this, "Received", Android.Widget.ToastLength.Short).Show();

        }

    }
    private void receiveData()
    {
        //RECEIVE DATA VIA INTENT
        Intent i = Intent;
        string count = i.GetStringExtra("COUNT");             

        var textView =  FindViewById<Android.Widget.TextView>(Resource.Id.textView1);
        textView.Text = count;

        //SET DATA TO TEXTVIEWS
        //Android.Widget.TextView CountView = FindViewById<Android.Widget.TextView>(Resource.Id.itemCount);
        //CountView.SetText(count, Android.Widget.TextView.BufferType.Normal);
        //Android.Widget.Toast.MakeText(this, count, Android.Widget.ToastLength.Short).Show();
    }

}

Screenshot:

enter image description here

CodePudding user response:

I was able to solve it on my own, although my solution does not completely convince me, I'll post it in case someone has the same problem as me.

Fragment

//add the activity reference to the fragment
using static GumisaAPP.Activities.BaseActivity;


//we create the procedure that will be in charge of modifying the activity's TextView
private void sendData_CountItem()
 {
        string count;
        Variables.Contador_Items = Variables.Contador_Items   1;
        count= Convert.ToString(Variables.Contador_Items);
//we access the texview of the activity from the fragment
        TextView textCount = Activity.FindViewById<TextView>(Resource.Id.itemCount);
// modify the text of the TextView
        textCount.SetText(count, TextView.BufferType.Normal);
 }

// call the procedure
private void EnviarDatos(){
sendData_CountItem();
}

Thank you for taking the trouble to answer my question. Greetings

  • Related