Android fragment nested fragment problem solving solution

Source: Internet
Author: User

All said fragment easy to use, duang~~, and encountered problems, record, share to meet this problem classmate!

1.fragment getactivity () is null when nesting fragment

Activity a nested fragment b,b nested fragment c,c jump to activity D, when activity D is finished, C is prone to burst getactivity empty. If your activity is recycled, Then you need to save the fragment information in the bundle, my workaround: Fragment will be registered in the activity Fragmentmanager after instantiation, this action is encapsulated in fragment Onattach ( Activity) method, you can assign a value directly to the Onattach (activity activity) method

2.fragment nested fragment when there is no Onactivityresult method to perform sub-fragment

activity a nested fragment b,b nested fragment c,c jump to activity D via Startactivityforresult, and when activity D is finished, Did not execute C's Onactivityresult

In fact this problem is not everyone will encounter, when your activity A and fragment B do not rewrite the Onactivityresult method, may not appear this problem, but my activity a rewrite the Onactivityresult method, So when activity D is destroyed, go directly to the activity of the onactivityresult, in fact, the problem is very simple, but when encountered may not be able to imagine, The solution is to add Super.onactivityresult (Requestcode, ResultCode, data) to the Onactivityresult method in the activity; everything's OK.

Onactivityresult Method of activity:

@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult ( Requestcode, ResultCode, data), ...}

Fragment Method of Onactivityresult:

public void Onactivityresult (int requestcode, int resultcode, Intent data) {}

Of course, there are special circumstances do not go Onactivityresult method, encountered also do not fret, from the activity of the Onactivityresult method to manually call fragment method, the corresponding data passed through

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

Android fragment nested fragment problem solving solution

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.