[Android] startactivityforresult and onactivityresult

Source: Internet
Author: User

Http://www.eifr.com/article.php? Id = 1464

Startactivityforresult () directly calls onactivityresult ()
Developers know that you can use startactivityforresult () and onactivityresult () Methods to pass or receive parameters.
However, in the listener project, onactivityresult () is executed before the called activity returns.
After searching for a long time, I finally learned through the gossip that this is related to the launchmode of the activity. This attribute can be set in androidmanifest. xml.
Originally set it to singleinstance. After testing, all the activities that need to be passed or received cannot set this attribute, or they can only be set to standard mode. Otherwise, the system will set this attribute in startactivityforresult () directly call onactivityresult ().

Onactivityresult does not respond after startactivityforresult is called.
When two activities pass data and return data, the onactivityresult of the requester never responds. This method is not called in debug mode. I checked various configurations and program code and found no errors. After looking for a lot of information, I finally saw a friend saying that it was a problem to call the startactivityforresult parameter, that is, the call was like this:
Startactivityforresult (intent, 0 );
Is a problem with the second parameter. This parameter must be greater than 0 to be returned, and the onactivityresult method is activated.
At first, I used the default constant result_ OK of an activity. After tracking the code, I found that the value of this constant is-1. Of course, the onactivityresult method cannot be activated, then, modify it to an integer greater than 0, and the program runs successfully.
Startactivityforresult (intent, 1 ); //.


1. startactivityforresult (intent, int
Requestcode );

2. setresut (int
Resultcode, intent );

3. onactivityresult (int
Requestcode, int resultcode, intent );


Note:

1. startactivityforresult was recently used. An error occurred. After tracking, the activity was not started, but onactivityresult was directly executed. View the log and find

05-19 02:11:19. 822: Warn/activitymanager (67): activity is launching as a new task, so
Canceling activity result ."

I set the launchmode of the activity to be started to singletask.

Correct the launchmode of the target activity.

Conclusion: The launchmode of the target activity cannot be set to singletask.

2. The requestcode value is greater than or equal to 0. Otherwise, startactivityforresult is changed to startactivity.

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.