Android fragment nested fragment solution, androidfragment

Source: Internet
Author: User

Android fragment nested fragment solution, androidfragment

Fragment is easy to use, duang ~~, If you have encountered another problem, record it and share it with those who have encountered the problem!

1. When fragment is nested, getActivity () is null.

Activity A nested fragment B, B nested fragment C, C jump to activity D, when activity D is completed, it is easy to find that getActivity is empty in C. if your activity is recycled, you need to save the fragment information in the bundle. My solution: After the fragment is instantiated, it will be registered in the fragmentManager of the activity, this action is encapsulated in the onAttach (Activity activity) method in fragment. You can assign values directly in the onAttach (Activity activity) method.

2. When fragment is nested, The onActivityResult method of the sub-fragment is not executed.

Activity A is nested with fragment B and B is nested with fragment C. C jumps to activity D through startActivityForResult. After activity D is finished, onActivityResult of C is not executed.

In fact, not everyone will encounter this problem. When activity A and Fragment B do not rewrite the onActivityResult method, this problem may not occur, but activity A has rewritten the onActivityResult method, therefore, when activity D is destroyed, the onActivityResult of the activity is directly executed. In fact, this problem is also very simple, but it may not be imagined when it is encountered, the solution is to add super to the onActivityResult method of Activity. onActivityResult (requestCode, resultCode, data); everything is OK

OnActivityResult method of Activity:

@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);...}

The onActivityResult method of fragment:

public void onActivityResult(int requestCode, int resultCode, Intent data) {}

Of course, the onActivityResult method is not used in some special cases. Do not get bored when you encounter it. Manually call the fragment method from the onActivityResult method in the Activity and pass the corresponding data to it.

If you have a better way to communicate with each other!

Related Article

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.