Reputation: 12374
My app is simple. I have a main activity with a sort of dock at the bottom, with 4 icons: A, B, C, D.
When the apps starts, the icon A is active, and its contents is displayed on the screen. The contents of A is a ViewPager containing 3 screens that you can change by swiping horizontally.
So my main Activity is a FragmentActivity, which has a "content" framelayout, and a "dock" fragment. The framelayout is hence inflated with a viewpager.
Problem is, my ViewPager contains 3 Fragments. Hence, these 3 Fragments are nested into the content Fragment. If I try to replace the content Fragment (ie the ViewPager) with another Fragment (say the user clicked on icon B in the dock), it crashes because Android doesn't support nested Fragments.
I tried to change the main activity to a TabActivity, so that the 3 Fragments will be nested into an activity instead of a Fragment. That's great it works.
Except that TabActivity is deprecated, and I am advised to use... Fragments!
What could be done here?
Edit: let me paste the stack trace
01-26 00:03:41.167 E/AndroidRuntime(32723): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 01-26 00:03:41.167 E/AndroidRuntime(32723): at android.view.ViewGroup.addViewInner(ViewGroup.java:3344) 01-26 00:03:41.167 E/AndroidRuntime(32723): at android.view.ViewGroup.addView(ViewGroup.java:3215) 01-26 00:03:41.167 E/AndroidRuntime(32723): at android.view.ViewGroup.addView(ViewGroup.java:3172) 01-26 00:03:41.167 E/AndroidRuntime(32723): at android.view.ViewGroup.addView(ViewGroup.java:3152) 01-26 00:03:41.167 E/AndroidRuntime(32723): at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40) 01-26 00:03:41.167 E/AndroidRuntime(32723): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:874) 01-26 00:03:41.167 E/AndroidRuntime(32723): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080) 01-26 00:03:41.167 E/AndroidRuntime(32723): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622) 01-26 00:03:41.167 E/AndroidRuntime(32723): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416) 01-26 00:03:41.167 E/AndroidRuntime(32723): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420) 01-26 00:03:41.167 E/AndroidRuntime(32723): at android.os.Handler.handleCallback(Handler.java:605) 01-26 00:03:41.167 E/AndroidRuntime(32723): at android.os.Handler.dispatchMessage(Handler.java:92) 01-26 00:03:41.167 E/AndroidRuntime(32723): at android.os.Looper.loop(Looper.java:137) 01-26 00:03:41.167 E/AndroidRuntime(32723): at android.app.ActivityThread.main(ActivityThread.java:4340) 01-26 00:03:41.167 E/AndroidRuntime(32723): at java.lang.reflect.Method.invokeNative(Native Method) 01-26 00:03:41.167 E/AndroidRuntime(32723): at java.lang.reflect.Method.invoke(Method.java:511) 01-26 00:03:41.167 E/AndroidRuntime(32723): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 01-26 00:03:41.167 E/AndroidRuntime(32723): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 01-26 00:03:41.167 E/AndroidRuntime(32723): at dalvik.system.NativeStart.main(Native Method)
Edit2: more code
In the xml I have a FrameLayout
In my main activity onCreate:
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.main_contents, new HomeFragment());
ft.commit();
So the ViewPager is not implemented in the XML, but is programmatically added into a FrameLayout.
The crash happens when I'm trying to create a new transaction that .replace()
s the R.id.main_contents
with a new SurvivalFragment()
Upvotes: 1
Views: 1592
Reputation: 13154
Good news, nesting fragments is now supported in Jelly Bean and newest Support Package
Upvotes: 1
Reputation: 12374
I'm sorry. After searching a bit more, I found this: Error swapping / adding an XML Fragment
I just needed to set the last parameter of inflate to false. My bad.
EDIT: I'm still actually looking for a better alternative. I am using the deprecated TabActivity. I really want to use the Fragments API but I just can't do what I need to do.
Upvotes: 0