Working with fragments in an Android app

Adding fragments to an app
<android.support.constraint.ConstraintLayout
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”
android:id=”@+id/content_main”
app:layout_behavior=”@string/appbar_scrolling_view_behavior”
tools:context=
”com.example.nipuna.fragment_transaction_2.MainActivity”
tools:showIn=”@layout/activity_main”>
return inflater.inflate(R.layout.fragment_first, container, false); 
View v= inflater.inflate(R.layout.fragment_first, container, false);
return v;
FristFragment firstFragmentInstance=new FirstFragment();
FragmentManager firstFragmentManager=getSupportFragmentManager();
FragmentTransaction firstFragmentTransaction=firstFragmentManager.beginTransaction();
firstFragmentTransaction.replace(R.id.content_main,firstFragmentInstance,”first_fragment_tag”).addToBackStack(null).commit();
this.getSupportFragmentManager().beginTransaction().replace(R.id.content_main, new FirstFragment())
.addToBackStack(null).commit();
View v=inflater.inflate(R.layout.fragment_first,container,false);SecondFragment secondFragment=new SecondFragment();
Button OpenFrag2=v.findViewById(R.id.open_frag_2);

OpenFrag2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
getActivity().getSupportFragmentManager()
.beginTransaction().replace(R.id.content_main,new SecondFragment())
.addToBackStack(null).commit();
}
});
return v;
View v=inflater.inflate(R.layout.fragment_second,container,false);FirstFragment firstFragment=new FirstFragment();
Button OpenFrag1=v.findViewById(R.id.open_frag_1);
OpenFrag1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
getActivity().getSupportFragmentManager()
.beginTransaction().replace(R.id.content_main,new FirstFragment())
.addToBackStack(null).commit();
}
});
return v;

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store