Tenho uma Activity principal que tem UM frameLayout. O frameLayout inicia com o FragmentA. FragmentA tem um botão que chama FragmentB abrindo no mesmo frameLayout, substituindo o FragmentA. Como passar parâmetros do FragmentA para o FragmentB no mesmo framelayout e assim sucessivamente, caso tenha FragmentC, passando A para o B e B para o C?
Código
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 - Layout da MainActivity
<?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>