Since Android 3.0 introduced Fragment, the practice of using Activity to nest some Fragments has become more popular. This is indeed some advantages brought by Fragment. For example, Fragment can enable you to separate the activity into multiple reusable components, each with its own life cycle and UI. More importantly, Fragment solves the lack of smooth switching between activities and realizes a lightweight and switch. However, in the .v4 package provided by the official, there are still some bugs in Fragment to a greater or lesser extent. Today I will share with you these bugs and solutions.
Case 1:When using Fragment to nest other sub-Fragments, we need to manage sub-Fragments. At this time, we need to call ChildFragmentManager to manage these sub-Fragments. The possible exceptions may be mainly:
: No activity
First, let’s analyze the reasons for the occurrence of Exception:
Through DEBUG, when the Fragment is started from an Activity for the first time and then the Fragment is launched, there is a variable pointing to the Activity. However, when the Fragments are exited and then returned to the Activity, and then enter the Fragment, the variable becomes null. This is easy to understand why the thrown exception is No activity.
What causes this Exception? If you want to know the cause of the exception, you must look at the relevant code of the Fragment and find that the Fragment will be reset after detached, but it does not reset the ChildFragmentManager, so it will cause the ChildFragmentManager to be incorrect.
After finding the cause of the exception, it can be easily solved. We need to reset the ChildFragmentManager when the Fragment is detached, that is:
@Override public void onDetach() { (); try { Field childFragmentManager = .getDeclaredField("mChildFragmentManager"); (true); (this, null); } catch (NoSuchFieldException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
Case 2:When we start a Fragment from an Activity, and then instantiate some child Fragments in this Fragment, and another Activity is started in the child Fragment, that is, start by starting ActivityForResult, the phenomenon caused by the child Fragment will not receive the OnActivityResult. If it is started in a mode, only the Activity will receive the OnActivityResult. If it is started in a mode, only the parent Fragment can receive (the Activity can also receive it at this time), but no matter what, the child Fragment cannot receive the OnActivityResult.
This is a very strange phenomenon. Logically speaking, it should be that the child Fragment receives the OnActivityResult. What exactly caused it? This is because a certain code-writing employee complained that he did not pay the bonus, was a little lazy, and wrote less part of the code, and did not consider the situation of the Fragment nesting the Fragment.
Let's take a look at the code in FragmentActivity:
protected void onActivityResult(int requestCode, int resultCode, Intent data) { (); int index = requestCode >> 16; if (index != 0) { index--; if (( == null) || (index < 0) || (index >= ())) { ("FragmentActivity", "Activity result fragment index out of range: 0x" + (requestCode)); return; } Fragment frag = (Fragment)(index); if (frag == null) { ("FragmentActivity", "Activity result no fragment exists for index: 0x" + (requestCode)); } else { (requestCode & 0xFFFF, resultCode, data); } return; } (requestCode, resultCode, data); }
Obviously, the designer shifted the Fragment subscript + 1 left by 16 bits to mark whether the request is a Fragment, took the result and decoded the subscript, and directly took the corresponding Fragment. This did not consider making a map map of the Fragment nested Fragment, so this bug occurred.
However, if we need to deal with some things during OnActivityResult, we can start it in the child Fragment in the way and then receive data in the parent Fragment. We need to provide a method in the child Fragment, such as: getResultData (Object obj), call this method through the child Fragment instance in the parent Fragment, pass the corresponding data over, and then update the child Fragment.
The above is a bug that you may encounter when using Fragment to nest Fragment. After understanding the reasons for the bug, you can perfectly solve the problem. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!