how to pass data between a dialogfragment to activity

Viewed 136

I want to send data from the dialogfragment to activity by one command inside the button .

//code in dialogfragment
 ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(context);
                    ISharedPreferencesEditor editor = prefs.Edit();
                    editor.PutString("my_data", "some_data");
                    editor.Apply();

                    second = new SecondActivity();

                    String myData = prefs.GetString("my_data", "");
                    second.txtView.Text = myData; // textview in secondactivity

                    Dismiss();
1 Answers

According to your description, your need to create Dialogfragment firstly in your project, and load layout in your DialogmentFragment.

public class DialogFragment1: DialogFragment
{
    private TextView tv1;
    private TextView tv2;
    private Button btn1;

    public static DialogFragment1 NewInstance(Bundle bundle)
    {
        DialogFragment1 fragment = new DialogFragment1();
        fragment.Arguments = bundle;
        return fragment;
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        View view = inflater.Inflate(Resource.Layout.Signuplayout, container, false);
        tv1 = view.FindViewById<TextView>(Resource.Id.textView1);
        tv2= view.FindViewById<TextView>(Resource.Id.textView2);
        btn1 = view.FindViewById<Button>(Resource.Id.button1);

        btn1.Click += Btn1_Click;
        return view;
    }

    private void Btn1_Click(object sender, EventArgs e)
    {
        ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Context);
        ISharedPreferencesEditor editor = prefs.Edit();
        editor.PutString("my_data", tv1.Text);
        editor.Apply();

        DialogFragment1 _exportFragment = (DialogFragment1)FragmentManager.FindFragmentByTag("Dialog Fragment");
        if (_exportFragment != null)
        {
            _exportFragment.Dismiss();
        }

    }
}

The signuplayout.xml:

<?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">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:id="@+id/textView1" 
    android:hint="first name"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:id="@+id/textView2" 
    android:hint="Pasword"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:text="Sign up" 
    android:id="@+id/button1"/>
</LinearLayout>

Mainactivity.cs:

 private Button btn1;
    private Button btn2;

    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);
        btn1 = FindViewById<Button>(Resource.Id.button1);
        btn2 = FindViewById<Button>(Resource.Id.button2);

        btn1.Click += Btn1_Click;
        btn2.Click += Btn2_Click;
    }

    private void Btn2_Click(object sender, System.EventArgs e)
    {
        ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Application.Context);
        string myData = prefs.GetString("my_data", "");
    }

    private void Btn1_Click(object sender, System.EventArgs e)
    {
        FragmentTransaction transcation = FragmentManager.BeginTransaction();
        DialogFragment1 signup = new DialogFragment1();
        signup.Show(transcation, "Dialog Fragment");

    }

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">

   <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:id="@+id/button1" android:text="Sign up"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:text="get data" 
    android:id="@+id/button2"/>
</LinearLayout>

Clicking first Button in MainLayout to display DialogFragment, then clicking Button in DialogFragment to pass data to activity, finally, clicking the second Button in MainLayout to get data.

This is one sample that you can take a look:

https://github.com/CherryBu/dialogapp

Update:

Adding one public method in Mainactivity.cs to display data:

 public void getdata()
    {
        ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Application.Context);
        string myData = prefs.GetString("my_data", "");
        Toast.MakeText(Application.Context,myData,ToastLength.Long).Show();
    }

Then instantiate one Mainactivity, call getdata method in DialogFragment.cs:

static  MainActivity mactivity;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        View view = inflater.Inflate(Resource.Layout.Signuplayout, container, false);
        tv1 = view.FindViewById<TextView>(Resource.Id.textView1);
        tv2= view.FindViewById<TextView>(Resource.Id.textView2);
        btn1 = view.FindViewById<Button>(Resource.Id.button1);

        btn1.Click += Btn1_Click;
        mactivity = new MainActivity();
        return view;
    }

    private void Btn1_Click(object sender, EventArgs e)
    {
        ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Context);
        ISharedPreferencesEditor editor = prefs.Edit();
        editor.PutString("my_data", tv1.Text);
        editor.Apply();

        DialogFragment1 _exportFragment = (DialogFragment1)FragmentManager.FindFragmentByTag("Dialog Fragment");
        if (_exportFragment != null)
        {
            _exportFragment.Dismiss();
        }

        mactivity.getdata();


    }
Related