Android Fragment nested Fragment use bugs with the perfect solution _android

Source: Internet
Author: User

Since Android3.0 introduced fragment, it has become more popular to use activity to nest some fragment, which is indeed some of the advantages fragment brings, such as: fragment allows you to separate the activity into reusable components, each Has its own lifecycle and UI, and more importantly, fragment solves the problem of switching between the activity, and realizes a lightweight and a switch, But in the official ANDROID.SUPPORT.V4 package, fragment still have some bugs, and share these bugs and solutions for today.

Case 1: When using fragment to nest other sub-fragment, we need to manage the child fragment, which calls Childfragmentmanager to manage the child fragment. The main exception that may arise are:
Java.lang.IllegalStateException:No activity

First, let's analyze the causes of exception:

With debug, there are variables that point to activity when you first start fragment from an activity and then start the fragment, but when you exit these fragment, you return to the activity, And then entering the fragment, this variable becomes null, which makes it easy to understand why the exception that is thrown is no activity

What is the cause of this exception? If you want to know the cause of the exception, then you must go to see the fragment code, found that fragment after detached will be reset, but it did not reset to Childfragmentmanager, Therefore, it will cause Childfragmentmanager state error.

Finding the cause of the anomaly can be very easy to solve the problem, we need to fragment be detached to reset the Childfragmentmanager, namely:

@Override public
void Ondetach () {
super.ondetach ();
try {
Field Childfragmentmanager = Fragment.class
. Getdeclaredfield ("Mchildfragmentmanager");
Childfragmentmanager.setaccessible (true);
Childfragmentmanager.set (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 of the fragment in this fragment, there is a return in the child fragment that initiates another activity, That is, through the Startactivityforresult way to start, this time the phenomenon will be that the child fragment receive Onactivityresult, If the Getactivity.startactivityforresult is started in the fragment, then only the activity will receive the Onactivityresult if the Getparentfragment.startactivit Yforresult, then only the parent fragment can receive it (activity can also be received at this point), but no matter fragment receive Onactivityresult.

This is a very strange phenomenon, logically speaking, should be to let the son fragment received Onactivityresult, what is the cause of it? This is because a person who wrote code complained about not paying bonuses, a little lazy, less write a part of the code, did not take into account the fragment to nest fragment situation.

Let's look at the code in Fragmentactivity:

protected void Onactivityresult (int requestcode, int resultcode, Intent data)
{
This.mFragments.noteStateNotSaved ();
int index = Requestcode >>;
if (index!= 0) {
index--;
if ((this.mFragments.mActive = null) | | (Index < 0) | | (Index >= this.mFragments.mActive.size ()) {
LOG.W ("fragmentactivity", "activity result fragment index out of range:0x" + integer.tohexstring (Requestcode)); C8/>return;
}
Fragment Frag = (Fragment) this.mFragments.mActive.get (index);
if (Frag = = null) {
LOG.W ("fragmentactivity", "activity result no fragment exists for index:0x" + Integer.tohexstrin G (Requestcode));
}
else {
Frag.onactivityresult (Requestcode & 0xFFFF, ResultCode, data);
}
return;
}
Super.onactivityresult (Requestcode, ResultCode, data);
}

Obviously, the designer of the fragment subscript +1 left 16 digits to mark this request is not fragment, get result and then decode the subscript, directly take the corresponding fragment, This does not consider a map map to the fragment nesting fragment, so this bug occurs.

But if we need to deal with something in the Onactivityresult, We can start by Getparentfragment.startactivityforresult in the fragment, and then receive the data in the parent fragment, we need to provide a method in the child fragment, such as: Getresultdata (Object obj), through the parent fragment of the child fragment instance to call this method, the corresponding data passed, and then to update the child fragment.

These are the bugs that you may encounter when you use fragment to nest fragment, and you can solve the problem perfectly after you understand the cause of the bug. Hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.