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>