androidandroid-fragmentsandroid-viewpagerandroid-fragmentactivitychild-fragment

on callingback the viewpager fragment on back press got this error You must call removeView() on the child's parent first


i'm replacing Fragment1(which has a view pager) from its child Fragment1 with Fragment2.

 Fragment fragment = Childfragment1.this.getParentFragment();
    FragmentManager fragmentManager = fragment.getFragmentManager();

    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    Fragment2 Fragment2 = new Fragment2();


    fragmentTransaction.replace(R.id.cat_container, Fragment2 , "Fragment2 ");
    fragmentTransaction.addToBackStack("Fragment2 ");
    fragmentTransaction.commit();

its working fine but when press back button it crashes with the error

The specified child already has a parent. You must call removeView() on the child's parent first on super.onBackPressed(); line

@Override
public void onBackPressed() {


    int count = getFragmentManager().getBackStackEntryCount();


    if (count == 0) {


        super.onBackPressed();


    } else {


        getFragmentManager().popBackStack();

    }




}

here is my logcat error

 java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.view.ViewGroup.addViewInner(ViewGroup.java:5038)
    at android.view.ViewGroup.addView(ViewGroup.java:4869)
    at androidx.viewpager.widget.ViewPager.addView(ViewPager.java:1485)
    at android.view.ViewGroup.addView(ViewGroup.java:4809)
    at android.view.ViewGroup.addView(ViewGroup.java:4782)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:887)
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
    at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
    at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
    at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2624)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:904)
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
    at androidx.fragment.app.BackStackRecord.executePopOps(BackStackRecord.java:500)
    at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2076)
    at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
    at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
    at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManagerImpl.java:310)
    at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManagerImpl.java:253)
    at androidx.fragment.app.FragmentManagerImpl.handleOnBackPressed(FragmentManagerImpl.java:233)
    at androidx.fragment.app.FragmentManagerImpl$1.handleOnBackPressed(FragmentManagerImpl.java:108)
    at androidx.activity.OnBackPressedDispatcher.onBackPressed(OnBackPressedDispatcher.java:189)
    at androidx.activity.ComponentActivity.onBackPressed(ComponentActivity.java:286)
    at in.arkemtech.offersoffer.main_freg.Main2Activity.onBackPressed(Main2Activity.java:466)
    at android.app.Activity.onKeyUp(Activity.java:3147)
    at android.view.KeyEvent.dispatch(KeyEvent.java:2744)
    at android.app.Activity.dispatchKeyEvent(Activity.java:3430)
    at androidx.core.app.ComponentActivity.superDispatchKeyEvent(ComponentActivity.java:115)
    at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:84)
    at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:133)
    at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:558)
    at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
    at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:2814)
    at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:346)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:5213)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5081)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4600)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4653)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4619)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4759)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4627)
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4816)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4600)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4653)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4619)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:

calling fragment fron viewpager


Solution

  • Here's an updated answer, as I predicted the other fix caused a knock on problem:

    This was triggered in a previously working App when I upgraded com.google.android.material from 1.1.0-alpha05 to 1.2.0-alpha04, a bit of experimenting shows problems with anything above 1.1.0-alpha07. So try reverting back to this version or earlier if you don't like the alpha's

    implementation 'com.google.android.material:material:1.1.0-alpha07'
    

    I'm still not sure if this is actually a bug in com.google.android.material, or whether the upgraded library uncovered a lurking flaw in my code.


    Old Answer below as it may still provide someone a useful clue:

    In my Fragments onCreateView I'd being stashing the created view

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (searchView == null) {
            searchView = super.onCreateView(inflater, container, savedInstanceState)
            ...
        }
        return searchView
    }
    

    I removed the code doing this and that's fixed the problem.

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val searchView = super.onCreateView(inflater, container, savedInstanceState)
        ...
        return searchView
    }
    

    I'm trying to remember now why I was stashing the view away in the first place, sure I'm soon going to find something else in my App is broken now.