Solve the problem of passing the request code when the activity is started in fragment

Source: Internet
Author: User

Resolves an issue that passes the request code when the activity is started in fragment:

The first thing to point out is that fragment has startactivityforresult methods, and there are startactivityforresult methods in activity: the difference between the two is:

The Startactivityforresult method in fragment:


The Startactivityforresult method in Fragmentactivity:


It can be seen from the official explanation that the Startactivityforresult method in fragment is called activity, The Startactivityforresult method in Fragmentactivity is to rewrite the startactivityforresult in the activity. So we call fragment's Startactivityforresult method, and when the activated activity returns, the Onactivityresult method in Fragmentactivity is called but cannot get the correct request code, If you call the Startactivityforresult method in Fragmentactivity, the fragment Onactivityresult method is not invoked when the activated activity returns.

Summarize:

If you want to start activity in fragment and ask for results, there are two result scenarios:

First: Call the Startactivityforresult method of fragment, and then process the returned request in fragment's Onactivityresult method.

The second is to obtain the Fragmentactivity object where the fragment is located in fragment through the Getactivity () method. Call the Activity object's Startactivityforresult method to start the activity, and then process the returned request in the Fragmentactivity Onactivityresult method.

To resolve the problem of passing the request code when the activity is started in fragment

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.