1

Basically this question has been asked many of times here and here.Solution given is not working for me. In some corrent answers were not posted by user. I want to have nested fragments. My scenario is as below :

I have one MainActivity, ProductEnterFrag, and ProductListFrag,ProductDetailFrag.

I have add the ProductEnterFrag to MainActivity dynamically by the FragmentManager. Like this :

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, new ProductEnterFrag());
ft.commit();

ProductEnter layout is as below :

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/fragContainer"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />

I have added the ProductListFrag to ProductEnterFrag like this :

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ProductListFrag(new ProductListFrag());
}

public void ProductListFrag(SherlockFragment frag) {
    getChildFragmentManager().beginTransaction().add(R.id.fragContainer, frag).commit();
    getChildFragmentManager().executePendingTransactions();`

}

ProductListFrag whose layout is, as below :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           android:text="product1" />
</LinearLayout>

Now on click of button, I want to add a another fragment say ProductDetailFrag,which should be another child fragment. When I am trying to add ProductDetailFrag from ProductListFrag like this :

new ProductEnterFrag().ProductListFrag(new ProductDetailsFrag());

It is throwing me : java.lang.IllegalStateException: Activity has been destroyed

I am new in nested fragments. Please help me where I am doing wrong. I have stuck with this problem from last 2 days.

Community
  • 1
  • 1
Dory
  • 7,462
  • 7
  • 33
  • 55
  • Is there a reason for creating a new instance of `ProductEnterFrag` when adding the `ProductDetailsFrag`? That new instance is not attached to an activity so any fragment transaction will fail. – user Nov 27 '13 at 18:15
  • @Luksprog then how should i access the function from ProductEnterFrag which creates adds fragment to childFragmentManager. I am new in this. May be I have done something wrong. please guide – Dory Nov 27 '13 at 18:17
  • 1
    Well I don't know your code, but I'm assuming that you're calling `new ProductEnterFrag().ProductListFrag(new ProductDetailsFrag());` from somewhere in the `ProductListFrag`. In `ProductListFrag` you do have a reference to the wrapper `ProductEnterFrag` through `getParentFragment()`, so if you're in `ProductListFrag` do: `((ProductEnterFrag).getParentFragment()).ProductListFrag(new ProductDetailsFrag());` – user Nov 27 '13 at 18:22
  • @Luksprog tonnes of thnks... it works. but I have one doubt, when I backpress from the ProductDetailFrag i should move to ProductListFrag. But this is moving me out of application. How could I handle this. – Dory Nov 27 '13 at 18:30
  • Your `ProductListFrag` method doesn't add the transaction to the backstack with `addToBackStack(null)`. – user Nov 27 '13 at 18:44
  • @Luksprog i added `addToBackStack(null)` in that method, though it is move me out of app – Dory Nov 27 '13 at 18:57
  • Override `onBackPressed()` in the `MainActivity`. In that method get a reference to the `ProductEnterFrag`, and use `getChildFragmentManager().getBackStackEntryCount()` on it. If that call returns a value bigger then 0 then do `getChildFragmentManager().popBackStack()` otherwise just call `super.onBackPressed()`; – user Nov 27 '13 at 20:01
  • @Luksprog it works.. thnks again.. – Dory Nov 28 '13 at 02:38
  • @nads Your issue resolved ? – Hardik Joshi Nov 28 '13 at 04:16
  • @Luksprog sorry bothering you again. But when i press back,one child fragment in poped up say `ProdDetailFrag` and the `ProdListFrag` is visible. And again when i tap on button in `prodListFrag` to open Detail frag. It is throwing me exception `java.lang.NullPointerException` `at android.support.v4.app.BackStackRecord.run` `at android.support.v4.app.FragmentManagerImpl.execPendingActions` could you please guide me where i m wrong. – Dory Nov 28 '13 at 07:20
  • Well, you shouldn't call `getChildFragmentManager().executePendingTransactions();`. Try with out it. – user Nov 28 '13 at 10:00
  • @Luksprog i m not calling `getChildFragmentManager().executePendingTransactions();` – Dory Nov 28 '13 at 11:10
  • Post a new question providing more details and the entire stacktrace of the exception. – user Nov 28 '13 at 11:32
  • @Luksprog I am having one doubt for structure of nested fragments. I have created `ProductEnterFrag` a wrapper class to add the child frags of `ProductList`. If I have other menu in `MainActivity` like `ProductList` menu,which has child fragments. Other menu also can have nested fragment. So can I use same Wrapper class `ProductEnterFrag` for all menus or create different wrapper class for each menu[which has nested fragments]. – Dory Jan 28 '14 at 10:49
  • I'm no sure I understand your problem but yes, if the wrapper fragment allows it you you may reuse it in other places. – user Jan 28 '14 at 14:34

0 Answers0