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>