Call startActivityForResult in a non-activity class,

Source: Internet
Author: User

Call startActivityForResult in a non-activity class,

For this question, I spent the afternoon not saying that I don't know how to call it, but that I used a seemingly correct call method, which is actually a pitfall.

I used the following method:

((Activity) mContext).startActivityForResult

This method is correct and context must be converted to Activity.

However, because I used it in a special scenario, the onActivityResult of the activity is not called back.


Next let's talk about my scenarios:

I created a DialogActivity in MainActivity and called the start () method of utils in DialogActivity. The statement in this method is (Activity) mContext ). startActivityForResult, while onActivityResult in DialogActivity is not called back.


After debugging for half a day, we found that the context of the start method of DialogActivity that is passed into utils belongs to MainActivity, because DialogActivity is started in MainActivity. In this case, MainActivity calls startActivityForResult. The onActivityResult of MainAcvtivity will be called back, but the result will not.


Originally, there was a DialogActivity between MainAcvtivity and the Activity to be started, and the returned result was returned to DialogActivity. Therefore, the onActivityResult of MainActivity will not be called back.


Solution: Do not upload context to call or upload activity

CONCLUSION: (Activity) mContext). startActivityForResult, which Activity the context belongs to, is the activity that calls the method and wants onActivityResult to get the callback. The two activities cannot be separated by other activities.


Respect labor results, reprinted please indicate the source:

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: 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.