Now create three fragment FragmentOne.Java, FragmentTwo.Java and FragmentThree.Java and update to below code.
FragmentOne.Javapackage com.pankaj.fragment.demo.fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.pankaj.fragment.demo.R;
import com.pankaj.fragment.demo.inner_fragments_for_fragment_one.FragmentInnerA;
import com.pankaj.fragment.demo.utils.utils;
/**
* Created by android_studio on 23/12/16.
*/
public class FragmentOne extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//________create the object of main view for FragmentOne
View v = inflater.inflate(R.layout.fragment_one, null);
Button innerFragmentA = (Button) v.findViewById(R.id.innerFragmentA);
//________add listener to innerFragmentA button
innerFragmentA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//_________replace the fragment to inner fragment
utils.replaceFragment(new FragmentInnerA(), getActivity().getSupportFragmentManager() );
}
});
//________return the object of main view for FragmentOne
return v;
}
}
FragmentTwo.Java
package com.pankaj.fragment.demo.fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.pankaj.fragment.demo.R;
import com.pankaj.fragment.demo.inner_fragment_for_fragment_two.FragmentTwoInnerA;
import com.pankaj.fragment.demo.utils.utils;
/**
* Created by android_studio on 23/12/16.
*/
public class FragmentTwo extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//________create the object of main view for FragmentTwo
View v = inflater.inflate(R.layout.fragment_two, null);
//________create the object of button innerFragmentA
Button FragmentTwoInnerFragmentA = (Button) v.findViewById(R.id.FragmentTwoInnerFragmentA);
//________add listener to FragmentTwoInnerFragmentA button
FragmentTwoInnerFragmentA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//_________replace the fragment to inner fragment
utils.replaceFragment(new FragmentTwoInnerA(), getActivity().getSupportFragmentManager());
}
});
//________return the object of main view for FragmentTwo
return v;
}
}
FragmentThree.Java
package com.pankaj.fragment.demo.fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.pankaj.fragment.demo.R;
import com.pankaj.fragment.demo.inner_fragment_for_fragment_three.FragmentThreeInnerA;
import com.pankaj.fragment.demo.inner_fragment_for_fragment_two.FragmentTwoInnerA;
import com.pankaj.fragment.demo.utils.utils;
/**
* Created by android_studio on 23/12/16.
*/
public class FragmentThree extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//________create the object of main view for FragmentTwo
View v = inflater.inflate(R.layout.fragment_three, null);
//________create the object of button FragmentThreeInnerFragmentA
Button FragmentThreeInnerFragmentA = (Button) v.findViewById(R.id.FragmentThreeInnerFragmentA);
//________add listener to FragmentThreeInnerFragmentA button
FragmentThreeInnerFragmentA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//_________replace the fragment to inner fragment
utils.replaceFragment(new FragmentThreeInnerA(), getActivity().getSupportFragmentManager());
}
});
//________return the object of main view for FragmentThree
return v;
}
}
Now create Fragments to open inside the fragments
FragmentOneInnerA.Javapackage com.pankaj.fragment.demo.inner_fragments_for_fragment_one;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.pankaj.fragment.demo.R;
import com.pankaj.fragment.demo.inner_fragment_for_fragment_three.FragmentThreeInnerB;
import com.pankaj.fragment.demo.utils.utils;
/**
* Created by android_studio on 23/12/16.
*/
public class FragmenOnetInnerA extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//________create the object of main view for FragmenOnetInnerA
View v = inflater.inflate(R.layout.fragment_inner_a, null);
//________create the object of button FragmentOneInnerFragmentB
Button FragmentOneInnerFragmentB = (Button) v.findViewById(R.id.FragmentOneInnerFragmentB);
//________add listener to FragmentOneInnerFragmentB button
FragmentOneInnerFragmentB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//_________replace the fragment to inner fragment
utils.replaceFragment(new FragmentOneInnerB(), getActivity().getSupportFragmentManager());
}
});
//________return the object of main view for FragmenOnetInnerA
return v;
}
}
fragment_one_inner_a.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment One Inner Fragment A" />
<Button
android:id="@+id/FragmentOneInnerFragmentB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment One Inner Fragment B" />
</LinearLayout>
FragmentOneInnerB.Javapackage com.pankaj.fragment.demo.inner_fragments_for_fragment_one;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.pankaj.fragment.demo.R;
/**
* Created by android_studio on 23/12/16.
*/
public class FragmentOneInnerB extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//________create the object of main view for FragmentOneInnerB
View v = inflater.inflate(R.layout.fragment_one_inner_b, null);
//________return the object of main view for FragmentOneInnerB
return v;
}
}
fragment_one_inner_b.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment One Inner Fragment B" />
</LinearLayout>
FragmentTwoInnerA.Javapackage com.pankaj.fragment.demo.inner_fragment_for_fragment_two;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.pankaj.fragment.demo.R;
import com.pankaj.fragment.demo.utils.utils;
/**
* Created by android_studio on 23/12/16.
*/
public class FragmentTwoInnerA extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//________create the object of main view for FragmentTwoInnerA
View v = inflater.inflate(R.layout.fragment_two_inner_a, null);
//________return the object of main view for FragmentTwoInnerA
return v;
}
}
FragmentThree.Java<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment Two Inner Fragment A" />
</LinearLayout>
FragmentThreeInnerA.Javapackage com.pankaj.fragment.demo.inner_fragment_for_fragment_three;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.pankaj.fragment.demo.R;
import com.pankaj.fragment.demo.utils.utils;
/**
* Created by android_studio on 23/12/16.
*/
public class FragmentThreeInnerA extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//________create the object of main view for FragmentThreeInnerA
View v = inflater.inflate(R.layout.fragment_three_inner_a, null);
//________create the object of button FragmentThreeInnerFragmentB
Button FragmentThreeInnerFragmentB = (Button) v.findViewById(R.id.FragmentThreeInnerFragmentB);
//________add listener to FragmentThreeInnerFragmentB button
FragmentThreeInnerFragmentB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//_________replace the fragment to inner fragment
utils.replaceFragment(new FragmentThreeInnerB(), getActivity().getSupportFragmentManager());
}
});
//________return the object of main view for FragmentThreeInnerA
return v;
}
}
fragment_three_inner_a.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment three Inner Fragment A" />
<Button
android:id="@+id/FragmentThreeInnerFragmentB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment Three Inner Fragment B" />
</LinearLayout>
FragmentThreeInnerB.Javapackage com.pankaj.fragment.demo.inner_fragment_for_fragment_three;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.pankaj.fragment.demo.R;
/**
* Created by android_studio on 23/12/16.
*/
public class FragmentThreeInnerB extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//________create the object of main view for FragmentThreeInnerB
View v = inflater.inflate(R.layout.fragment_three_inner_b, null);
//________return the object of main view for FragmentThreeInnerB
return v;
}
}
fragment_three_inner_b.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment Three Inner Fragment B" />
</LinearLayout>