首页 > 解决方案 > Changing Fragment From within another Fragment Android Java

问题描述

I'm working with fragments and I've encountered an exception when trying to change the fragment from current fragment. The basic design is:

  1. An activity with a frame layout.
  2. Fragment A that is initially placed in the frame layout of activity.
  3. Fragment A layout has a frame layout lets say frame_layout_a.
  4. Child_Fragment_A that is initially loaded in frame_layout_a.

The main design idea is to press a button Child_Fragment_A that loads Child_Fragment_B in the frame_layout_a. Currently in the On Click Listener for the button in Child_Fragment_A, I'm using the following code for this purpose

if (v.getParent() != null) {
            FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.child_fragment, new searchResults());
            transaction.addToBackStack(null);
            transaction.commit();
            Log.d("mActivity", "loadfragment()");
        }
        else{
            ((ViewGroup)v.getParent()).removeView(v);
            FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.child_fragment, new searchResults());
            transaction.addToBackStack(null);
            transaction.commit();
            Log.d("mActivity", "loadfragment()");
        }

This onClick() is implemented in the OnCreateView of Child_Fragment_A. However, this leads to the following exception:

Exception

D/AndroidRuntime: Shutting down VM
    
    
    --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.rehnsehan, PID: 2114
    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:5235)
        at android.view.ViewGroup.addView(ViewGroup.java:5064)
        at android.view.ViewGroup.addView(ViewGroup.java:5004)
        at android.view.ViewGroup.addView(ViewGroup.java:4976)
        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.BackStackRecord.executeOps(BackStackRecord.java:439)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Hopefully I was able to explain my problem sufficiently, Kindly point out what I'm missing or doing wrong here. Thanks.

标签: javaandroidandroid-fragmentsfragmentillegalstateexception

解决方案


推荐阅读