2

I am trying to create an activity with a navigation drawer. At first it had this onNavigationDrawerItemSelected method and it worked fine:

 @Override
    public void onNavigationDrawerItemSelected(final int position)
    {
        Fragment f = null;
        FragmentManager fragmentManager = getSupportFragmentManager();
        if (position == MainFragments.SOME_FRAGMENT.ordinal())
        {
            f = new SomeFragment();
            TabsFragmentUtils.replaceFragmentInContainer(fragmentManager, f, true);
        }

    }

The above index of MainFragments.SOME_FRAGMENT is 1 so the code never tried to create the fragment on startup.

After that I changed the method to this:

    @Override
    public void onNavigationDrawerItemSelected(final int position)
    {
        Fragment f = null;
        FragmentManager fragmentManager = getSupportFragmentManager();
        if (position == MainFragments.MAIN_FRAGMENT.ordinal())
        {
            f = new MainFragment();
        } else if (position == MainFragments.SOME_FRAGMENT.ordinal())
        {
            f = new SomeFragment();
        }
        TabsFragmentUtils.replaceFragmentInContainer(fragmentManager, f, true);
    }

Now the MainFragments.MAIN_FRAGMENT is of index 0 and I see this code is trying to run and replace the container and then I get an exception: Error inflating class fragment (probably the navigation drawer) caused by Recursive entry to executePendingTransaction.

After debugging I realized the code doesn't go through the MainFragemnt onCreateView method which is initializing the fragments tabs.

What can be done about it?

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
  • are you using getActivity().getSupportFragmentManager() in MainFragment? – Florin T. May 27 '15 at 14:12
  • yes it is when its trying to initialize tabs. but like i said, it doesn't even reach that code when I debug it. – CodeMonkey May 27 '15 at 14:31
  • If i understand right, you are using a fragment that has tabs with other fragments inside. For this you should call getChildFragmentManager() [for nested fragments]... that was my idea at first... but if you say onCreateView isn't called, than this doesn't help :) – Florin T. May 27 '15 at 14:43
  • You are right about the fragment with tabs which has other fragments inside the tabs. And also right about the method not being called. Is it possible that the drawer fragment needs to initialize but it happens only after setContentView which may cause the exception? – CodeMonkey May 27 '15 at 14:51
  • I manage to solve it pretty easily with a boolean flag which tells me if im still on the onCreate method and if I do simply won't do anything. but it sounds like avoiding the real issue. – CodeMonkey May 27 '15 at 14:57

0 Answers0