[android-developers] Re: Fragment state (and view) not restored when using ViewPager with nested fragments
Hi! So, I checked onAttachActivity and onDetach and this is very puzzling. onAttach is called, I store the reference to the activity, but onDetach is not called, and when I try to call getActivity(), or reference the stored activity, I always get null back. So that is the reason FragmentManager is failing as well, only I don't know why the activity is missing...? If I dump the fragment manager, I see that the activity is there (see emphasis): Active Fragments in 414896e0: #0: null #1: ConnectFragment{41505ae0 #1 id=0x7f060001} mFragmentId=#7f060001 mContainerId=#7f060001 mTag=null mState=5 mIndex=1 mWho=android:fragment:1 mBackStackNesting=0 mAdded=true mRemoving=false mResumed=true mFromLayout=false mInLayout=false mHidden=false mDetached=false mMenuVisible=false mHasMenu=false mRetainInstance=false mRetaining=false mUserVisibleHint=false mFragmentManager=FragmentManager{414896e0 in MainActivity{41489470}} mActivity=com.islonline.isllightdesign.MainActivity@41489470 mContainer=android.support.v4.view.ViewPager{4145c668 VFED 0,0-800,1033 #7f060001 app:id/pager} mView=android.support.v4.app.NoSaveStateFrameLayout{414b1258 V.E. 800,0-1600,1033} mInnerView=android.support.v4.app.NoSaveStateFrameLayout{414b1258 V.E. 800,0-1600,1033} #2: MainFilesFragment{4155a430 #2 id=0x7f060001} mFragmentId=#7f060001 mContainerId=#7f060001 mTag=null mState=5 mIndex=2 mWho=android:fragment:2 mBackStackNesting=0 mAdded=true mRemoving=false mResumed=true mFromLayout=false mInLayout=false mHidden=false *mDetached=false* mMenuVisible=true mHasMenu=false mRetainInstance=false mRetaining=false mUserVisibleHint=true mFragmentManager=FragmentManager{414896e0 in MainActivity{41489470}} *mActivity=com.islonline.isllightdesign.MainActivity@41489470* mContainer=android.support.v4.view.ViewPager{4145c668 VFED 0,0-800,1033 #7f060001 app:id/pager} mView=android.support.v4.app.NoSaveStateFrameLayout{4155bcf8 V.E. 1600,0-2400,1033} mInnerView=android.support.v4.app.NoSaveStateFrameLayout{4155bcf8 V.E. 1600,0-2400,1033} Child FragmentManager{4155b598 in MainFilesFragment{4155a430}}: Active Fragments in 4155b598: #0: ComputerListFragment{4155b7b8 #0 id=0x7f060010 COMPUTER_LIST} mFragmentId=#7f060010 mContainerId=#7f060010 mTag=COMPUTER_LIST mState=1 mIndex=0 mWho=android:fragment:2:0 mBackStackNesting=1 mAdded=false mRemoving=true mResumed=false mFromLayout=false mInLayout=false mHidden=false mDetached=false mMenuVisible=true mHasMenu=false mRetainInstance=false mRetaining=false mUserVisibleHint=true mFragmentManager=FragmentManager{4155b598 in MainFilesFragment{4155a430}} *mActivity=com.islonline.isllightdesign.MainActivity@41489470* mParentFragment=MainFilesFragment{4155a430 #2 id=0x7f060001} mSavedViewState=android.util.SparseArray@41690a60 #1: FileShareBrowserFragment{41690840 #1 id=0x7f060010} mFragmentId=#7f060010 mContainerId=#7f060010 mTag=null mState=5 mIndex=1 mWho=android:fragment:2:1 mBackStackNesting=1 mAdded=true mRemoving=false mResumed=true mFromLayout=false mInLayout=false mHidden=false mDetached=false mMenuVisible=true mHasMenu=false mRetainInstance=false mRetaining=false mUserVisibleHint=true mFragmentManager=FragmentManager{4155b598 in MainFilesFragment{4155a430}} *mActivity=com.islonline.isllightdesign.MainActivity@41489470* mParentFragment=MainFilesFragment{4155a430 #2 id=0x7f060001} mContainer=android.widget.FrameLayout{4155b210 V.E. 0,0-800,1033 #7f060010 app:id/file_browser_container} mView=android.support.v4.app.NoSaveStateFrameLayout{41698148 V.E. 0,0-800,1033} mInnerView=android.support.v4.app.NoSaveStateFrameLayout{41698148 V.E. 0,0-800,1033} #2: null Added Fragments: #0: FileShareBrowserFragment{41690840 #1 id=0x7f060010} Back Stack: #0: BackStackEntry{41690780 #0} mName=null mIndex=0 mCommitted=true Operations: Op #0: REPLACE FileShareBrowserFragment{41690840 #1 id=0x7f060010} Removed: ComputerListFragment{4155b7b8 #0 id=0x7f060010 COMPUTER_LIST} Back Stack Indices: #0: BackStackEntry{41690780 #0} Any help or pointers on what to look for would be appreciated! Thanks! Miha On Thursday, May 23, 2013 9:51:07 PM UTC+2, Miha wrote: Hi Streets Of Boston! On Thursday, May 23, 2013 4:16:22 PM UTC+2, Streets Of Boston wrote: When using fragments inside a fragment, you should use the fragment's ChildFragmentManager and not the (activity's) main FragmentManager. You are right. It's working when using child fragment manager, but the backstack inside the child fragment
[android-developers] Re: Fragment state (and view) not restored when using ViewPager with nested fragments
I checked the childFragmentManager state, and surprisingly, it is empty. So, the fragmentmanager dump output in previous posting shows all of the fragments, but when doing the same with child fragment manager, the output is: FragmentManager misc state: mActivity=null mContainer=android.support.v4.app.Fragment$1@41603ca0 mParent=MainFilesFragment{4169ccf0} mCurState=0 mStateSaved=false mDestroyed=false Why is that? Regards, Miha. On Friday, May 24, 2013 10:54:12 AM UTC+2, Miha wrote: Any help or pointers on what to look for would be appreciated! -- -- 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.
[android-developers] Re: Fragment state (and view) not restored when using ViewPager with nested fragments
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.
[android-developers] Re: Fragment state (and view) not restored when using ViewPager with nested fragments
Hi Streets Of Boston! On Thursday, May 23, 2013 4:16:22 PM UTC+2, Streets Of Boston wrote: When using fragments inside a fragment, you should use the fragment's ChildFragmentManager and not the (activity's) main FragmentManager. You are right. It's working when using child fragment manager, but the backstack inside the child fragment manager is not working. For instance, I'm replacing fragments based on user interaction, and when I call popBackStackImmediate(), the runtime throws NullPointerException: android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1419) If I use popBackStack() instead of it's *Immediate sidekick, I get an IllegalStateException: Activity has been destroyed: android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1342) That was the reason I wen't with activity's fragment manager in the beginning... Any ideas what can be wrong? The main fragment in a viewpager inflates a layout with one frame layout inside. I'm addin child fragments to that frame layout, and calling addToBackStack(null) in the process. Regards, Miha. -- -- 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.