How to pass parameters from one Fragment to another Fragment in the same FrameLayout?

1

I have a main Activity that has A frameLayout. FrameLayout starts with FragmentA. FragmentA has a button that calls FragmentB opening in the same frameLayout, replacing FragmentA. How to pass parameters from FragmentA to FragmentB in the same framelayout and so on, if you have FragmentC, passing A to B and B to C?

Code

MainActivity:

public class MainActivity extends AppCompatActivity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(R.id.frameInicial, new FragmentA(), "NewFragmentTag");
        ft.commit();
    }
}

FragmentA:

public class FragmentA extends Fragment  {

    public FragmentA() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_a, container, false);

        Button btChamaFragmentB = view.findViewById(R.id.btChamaFragmentB);

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

                final FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.frameInicial, new FragmentB(), "NewFragmentTag");
                ft.commit();

            }
        });

        return view;
    }
}

FragmentB

public class FragmentB extends Fragment {

    public FragmentB() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_b, container, false);
        return view;
    }
}

XML - MainActivity Layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context="aig.example.com.comunicacaofragment.MainActivity">


    <FrameLayout
        android:id="@+id/frameInicial"
        android:name="aig.example.com.menudeslizante.Fragment1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
    
asked by anonymous 25.04.2018 / 15:03

1 answer

1

I can solve the problem and I'll share it with you.

To call another fragment passing parameter use the following code:

FragmentB fragmentB = new FragmentB(); Bundle bundle = new Bundle(); FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); bundle.putString("ano", "2018"); fragmentB.setArguments(bundle); fragmentTransaction.replace(R.id.frameInicial, fragmentB ,"NewFragmentTag"); fragmentTransaction.commit();

To receive the parameter in the new fragment use the following code:

   ' Bundle mBundle = new Bundle();
    if(mBundle != null){
        mBundle = getArguments();
        ano = mBundle.getString("ano");'

    }
    
03.05.2018 / 14:24