Come passare valori tra frammenti

Un frammento può comunicare facilmente con i frammenti allegati alla stessa attività tramite  FragmentManager chiamando il getFragmentManager()metodo che ci permette di gestire le transazioni.
In questo esempio andiamo a passare un valore da un frammento ad un altro e andiamo a  chiamare il metodo addToBackStack() che ci permette di tornare indietro premendo il pulsante stack:

Activity_main.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:gravity=”center”
android:orientation=”vertical”>

<FrameLayout
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
    android:id=”@+id/frameLayout”/>
</LinearLayout>

Fragment_1.xml
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
    android:layout_width=”fill_parent”
    android:layout_height=”fill_parent”
    android:orientation=”vertical”>
   
<EditText
android:id=”@+id/edt”
android:layout_width=”match_parent”
android:layout_height=”60dp”

android:hint=”Enter Name”
android:layout_marginTop=”20dp”/>

<Button
android:id=”@+id/button”
android:layout_width=”200dp”
android:layout_height=”50dp”
android:text=”Pass Data”
        android:layout_gravity=”center” />
</LinearLayout>

Fragment_2.xml
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
    android:layout_width=”fill_parent”
    android:layout_height=”fill_parent”
    android:orientation=”vertical”>
   
<TextView
android:id=”@+id/txv”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
        android:textSize=”30sp”/>

<Button
android:id=”@+id/button”
android:text=”Back”
android:layout_width=”200dp”
android:layout_height=”50dp” />
</LinearLayout>

MainActivity.java
package com.example.passDataFragment.activities;

import android.app.Activity;
import android.os.Bundle;
import com.example.passDataFragment.R;
import android.app.*;
import android.widget.*;

public class MainActivity extends Activity {
FrameLayout frameLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout=findViewById(R.id.frameLayout);

FragmentTransaction transaction=getFragmentManager() .beginTransaction();

transaction.replace(R.id.frameLayout,new Fragment1 ()).commit();
}
}

Fragment1.java
package com.example.passDataFragment.activities;
import android.app.*;
import android.widget.*;
import android.os.*;
import android.view.*;
import com.example.passDataFragment.R;

public class Fragment1 extends Fragment
{
Button btn;
EditText txt;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_1, container, false);
txt=view.findViewById(R.id.edt);
btn=view.findViewById(R.id.button);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

String name=txt.getText().toString();


FragmentTransaction transaction=getFragmentManager() .beginTransaction();

transaction.addToBackStack(name);

     transaction.replace(R.id.frameLayout,new Fragment2(name)).commit();
  }
          
});

      return view;

}

}

Fragment2.java
package com.example.passDataFragment.activities;
import android.app.*;
import android.widget.*;
import com.example.passDataFragment.R;
import android.view.*;
import android.os.*;

public class Fragment2 extends Fragment
{
Button btn;
TextView txv;
String name;

public Fragment2(String name) {
this.name = name;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_2,container,   false);
txv=view.findViewById(R.id.txv);
btn=view.findViewById(R.id.button);

txv.setText(name);

btn.setOnClickListener(new View.OnClickListener() {
@Override
         public void onClick(View view) {


FragmentTransaction transaction=getFragmentManager() .beginTransaction();

transaction.addToBackStack(name);
transaction.replace(R.id.frameLayout, new Fragment1()).commit();

}
});

      return view;
}
}