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

Asked

Viewed 565 times

1

I have a main Activity that has A frameLayout. FrameLayout starts with Fragment. Fragmenta has a button that calls Fragmentb opening in the same frameLayout, replacing Fragmenta. How to pass Fragment parameters 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();
    }
}

Fragment:

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>

1 answer

1

manage to solve the problem and I will share with you.

To call another Fragment passing parameter use the code below:

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 code below:

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

    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.