java - 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:
- An activity with a frame layout.
- Fragment A that is initially placed in the frame layout of activity.
- Fragment A layout has a frame layout lets say frame_layout_a.
- 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.
解决方案
推荐阅读
- javascript - 从 nock 记录中删除敏感信息
- html - 如何使导航栏适合整页?
- php - PHPMyAdmin 在远程服务器上没有按预期工作。错误 500,未找到 $respond
- flutter - Flutter 丰富的内容本地化
- javascript - 删除每个列表项
- python - 将字典 id 字典转换为合理的 df
- laravel-livewire - 如何定义返回“N”的复选框未选中?
- jenkins - 如何在 Jenkins 管道中为下游作业设置 cron
- swift - 对 viewModel 的 Json 响应数组 - Swift 5
- reactjs - 如何强制重新渲染 React 函数组件