StartActivityForResult and onActivityResult

Source: Internet
Author: User
Tags return tag

The jump between androidactivities is not only for startActivity (Intent I), but also for startActivityForResult (Intent intent, int requestCode.

The onActivityResult (int requestCode, int resultCode, Intent data) method can be used to obtain the operation after the end of the Request Activity.
Note three methods: startActivityForResult (Intent intent, int requestCode), onActivityResult (int requestCode, int resultCode, Intent data), setResult (int resultCode, Intent data)

For example, the following code redirects From to ToB and ToC.
From:
If (condition ){
Intent intent = new Intent (this, ToB. class );
StartActivityForResult (serverIntent, REQUEST_CODE_01); // jump to ToB
} Else {
Intent intent = new Intent (this, ToC. class );
StartActivityForResult (serverIntent, REQUEST_CODE_02); // jump to ToC
}
 
Public void onActivityResult (int requestCode, int resultCode, Intent data ){
Switch (requestCode ){
Case REQUEST_CODE_01:
If (resultCode = Activity. RESULT_ OK)
// Do something
Break;
Case REQUEST_CODE_02:
// Do something
Break;
}
}
 
 
Here we will talk about the startActivityForResult (Intent intent, int requestCode) parameter. Needless to say, the first Intent is the second one. We use REQUEST_CODE_01 and REQUEST_CODE_02, in fact, this is an int type constant we have defined for marking. The specific function can be seen in the onActivityResult method to determine which Activity is returned from.
 
ToB:
Intent intent = new Intent ();
Intent. putExtra (key, value );
SetResult (Activity. RESULT_ OK, intent );
Finish (); // The result is returned to the From
 
ToC:
Intent intent = new Intent ();
Intent. putExtra (key, value );
SetResult (Activity. RESULT_ OK, intent );
Finish (); // The result is returned to the From
 
The first parameter of setResult corresponds to the second parameter of onActivityResult above. Do not confuse the first parameter of onActivityResult with the second parameter. One is the request tag and the other is the return tag.
 
Please add ~~~

 


From TQUDING's column

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.