6

I have a parent fragment ie FragmentA and inflating a child fragment FragmentB on it. In onCreateView function of FragmentB trying to inflate another fragment `FragmentC'.

FragmentA.java

public class FragmentA extends Fragment {

    private static final String TAG = "FragmentA";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                 Bundle savedInstanceState) {
    /* Inflate the layout for this fragment */
    View view = inflater.inflate(R.layout.root_fragment, container, false);


    FragmentManager manager = getChildFragmentManager();

    FragmentTransaction transaction = manager
        .beginTransaction();
    /*
     * When this container fragment is created, we fill it with our first
         * "real" fragment
         */
    transaction.replace(R.id.container_main, new FragmentRecharge());
    transaction.addToBackStack(null);
    transaction.commit();


    return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
    outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
    super.onSaveInstanceState(outState);
    }
}

FragmentB.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.recharge_layout_new, container,

    false);
....

try {
    ((ActivityMain) context).bannerView.setVisibility(View.GONE);
    Fragment fragment = new FragmentC();
//                fragment.setArguments(bundle);
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.container_main, fragment);
    fragmentTransaction.addToBackStack("my_fragment");
    fragmentTransaction.commit();
} catch (Exception e) {
    Crashlytics.logException(e);
}

 }  

FragmentC.java

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                 Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.recharge_layout_new, container,

    false);
    ....

Layout file:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/tools"
    android:id="@+id/parent_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/fragment_back_color">

     <FrameLayout
    android:id="@+id/container_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible">

    </FrameLayout>

but it get destroyed automatically and come to `FragmentB`.  How can I fix this issue ?

I am trying to use Handler with postDelayed with a 1 second interval then it is inflating FragmentC & it is working fine. This looks not a solution.

        new android.os.Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    ((ActivityMain) context).bannerView.setVisibility(View.GONE);
                    Fragment fragment = new FragmentC();
//                fragment.setArguments(bundle);
                    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.add(R.id.container_main, fragment);
                    fragmentTransaction.addToBackStack("my_fragment");
                    fragmentTransaction.commit();
                } catch (Exception e) {
                    Crashlytics.logException(e);
                }
            }
        }, 1000);
Kushminder Garg
  • 518
  • 1
  • 6
  • 14

4 Answers4

0

UPDATE I implement a test using a manual Nested Fragment Addition in mi Github Account

With current versions of the Android Support you can nest fragments, by means of getChildFragmentManager(). The basics behind childFragmentManager is that it defers loading until the previous fragment transaction has finished. There is another way to do it?

Sure! Nesting without ChildManager, but how? well, see below; We will have a deferred Fragment, that defers loading, so other Fragments can be loaded inside of it.

The idea is simple. However, the Handler is to find a really handy class that effectively waits for a space to execute on the main thread(Fragments are interfered with the UI so, they run on the main thread) after the current fragment transaction has finished committing.

private final Handler handler = new Handler();
private Runnable runPager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    return inflater.inflate(R.layout.recharge_layout_new, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
    runPager = new Runnable() {

        @Override
        public void run()
        {
          getFragmentManager().beginTransaction().addFragment(R.id.frag_container, FragmentC.newInstance()).commit();
        }
    };
    handler.post(runPager);
}

/**
 * @see android.support.v4.app.Fragment#onPause()
 */
@Override
public void onPause()
{
    super.onPause();
    handler.removeCallbacks(runPager);
}
Teocci
  • 7,189
  • 1
  • 50
  • 48
  • Not working. Same problem of fragment destroying.I have used in onActivityCreated() function. – Kushminder Garg Jun 09 '16 at 07:10
  • Let me think another solution to prevent the destruction of the fragment. Do you have the code of you onActivityCreated()? – Teocci Jun 16 '16 at 09:43
  • I updated the post there is a sample based on a Manual Addition of Nested Fragments. here is the link: https://github.com/teocci/NestedFragments – Teocci Jun 22 '16 at 07:11
0

I did it in my way its not destroying the Fragment C its working fine.

TestFragmentActivity.java

public class TestFragmentActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_test);

        FragmentA fragmentA = new FragmentA();
        FragmentTransaction mTransaction = getSupportFragmentManager().beginTransaction();
        mTransaction.replace(R.id.container_main, fragmentA).commit();
    }

}

FragmentA.java

public class FragmentA extends Fragment
{
    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        return view = inflater.inflate(R.layout.fragment_a,container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        FragmentB fragmentB = new FragmentB();
        FragmentTransaction mTransaction = getChildFragmentManager().beginTransaction();
        mTransaction.replace(R.id.xLinLayFragmentContentB, fragmentB).commit();

    }
}

FragmentB.java

public class FragmentB extends Fragment
{
    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        return view = inflater.inflate(R.layout.fragment_b,container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        FragmentC fragmentC = new FragmentC();
        FragmentTransaction mTransaction = getChildFragmentManager().beginTransaction();
        mTransaction.replace(R.id.xLinLayFragmentContentC, fragmentC).commit();
    }
}

FragmentC.java

public class FragmentC extends Fragment
{
    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        return view = inflater.inflate(R.layout.fragment_c,container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);


    }
}

activity_fragment_test.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/parent_layout"
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:card_view="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/container_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">

    </FrameLayout>

</RelativeLayout>

fragment_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 A"
        android:textSize="15sp"/>

    <LinearLayout
        android:id="@+id/xLinLayFragmentContentB"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:orientation="vertical"/>

</LinearLayout>

fragment_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 B"
        android:textSize="15sp"/>

    <LinearLayout
        android:id="@+id/xLinLayFragmentContentC"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:orientation="vertical"/>

</LinearLayout>

fragment_c.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 C"
        android:textSize="15sp"/>


</LinearLayout>

enter image description here

Kumar M
  • 994
  • 6
  • 21
0

You should call your nested fragment inside onActivityCreated() method just like this.

@Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        Fragment fragment = new FragmentC();
    //                  fragment.setArguments(bundle);
                    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.add(R.id.container_main, fragment);
                    fragmentTransaction.addToBackStack("my_fragment");
                    fragmentTransaction.commit();
}
Sanjay Kushwah
  • 190
  • 1
  • 9
0

Try to implement onDestroy() for default and if you have onAttach() in your fragment, then remove it, may be it will create problem. and I am using this fragment call for replace fragment. Try this one.May be it will help you:

    Fragment mFragment = null;
                    mFragment = new RadarMainActivity();
                    FragmentManager fragmentManager = getActivity()
                            .getSupportFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.container_body, mFragment).commit();
parik dhakan
  • 787
  • 4
  • 19