When using fragments inside a fragment, you should use the fragment's 
ChildFragmentManager and not the (activity's) main FragmentManager. 

On Thursday, May 23, 2013 9:12:31 AM UTC-4, Miha wrote:
>
> Hi!
>
> I'm using a ViewPager to swipe "between screens". One of the fragments is 
> composed of two fragments, so the hierarchy looks like:
>
> ViewPager -> FragmentStatePageAdapter - > [Frag1, Frag2, Frag3], where 
> Frag1's layout is composed of two FrameLayouts, to which two fragments are 
> added like this:
>
> if (savedInstanceState != null) {
>   FragmentManager supportFragmentManager = 
> getActivity().getSupportFragmentManager(); 
>   _computerListFragment = (ComputerListFragment) 
> supportFragmentManager.findFragmentByTag(LIST_FRAGMENT); 
>   if (_computerListFragment != null) { 
>     _computerListFragment.setOnComputerSelectedListener(this);
>   }
>
>   _computerDetailsFragment = (ComputerDetailsFragment) 
> supportFragmentManager.findFragmentByTag(DETAILS_FRAGMENT); 
>   if (_computerDetailsFragment != null) { 
>     _computerDetailsFragment.setOnComputerDetailsClickListener(this); 
>   }
>
>   return root; 
> }
>
> _computerListFragment = new ComputerListFragment(); 
> FragmentManager supportFragmentManager = 
> getActivity().getSupportFragmentManager(); 
> FragmentTransaction transaction = 
> supportFragmentManager.beginTransaction(); 
> transaction.add(R.id.computer_list_fragment, _computerListFragment, 
> LIST_FRAGMENT); 
>
> if (_isDualPane) { 
>   _computerDetailsFragment = new ComputerDetailsFragment(); 
>   
> _computerDetailsFragment.setOnComputerDetailsClickListener(MainComputerFragment.this);
>  
>   transaction.add(R.id.computer_details_fragment, 
> _computerDetailsFragment, DETAILS_FRAGMENT); 
> } 
> transaction.commit();
>
> This works well for orientation changes, the fragments get recreated 
> appropriately and onSaveInstanceState gets called for inner fragments 
> (above: ComputerListFragment and ComputerDetailsFragment), but when using 
> ViewPager, I see that:
>
> 1) onSaveInstanceState is called on the First fragment, but not on the 
> inner fragments (in above case, for ComputerListFragment and 
> ComputerDetailsFragment)
> 2) trying to call 
> getActivity().getSupportFragmentManager().saveFragmentInstanceState(_computerListFragment);
>  
> yields NullPointerException 
> in 
> android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1588)
> 3) if I try to to add them again, I get an IllegalStateException that the 
> fragment is already added (makes sense)
>
> It seems to me that I need to tell the fragmentManager that it should save 
> the fragment states, and then to restore them, but I see no appropriate 
> methods for this. It also seems to me that I would need to tell the 
> fragmentManager to show the fragments, since ViewPager obviously removes 
> them somehow...
>
> Or have I got it backwards somehow?
>
> Regards,
>  Miha.
>
> ps: I was using ChildFragmentManager at first, but then switched to 
> FragmentManager due to issue I was having. Could this be the case?
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to