Use startActivityForResult to return data in android

Source: Internet
Author: User

 

Suppose: Here I have two activities: A and B. When I transfer data from A to B, I use Bundle to encapsulate the data, and then jump from A to B, when B needs to encapsulate the data and send it back to A, it jumps back to. When receiving data in A, you must first determine whether the Bundle is empty, because the Bundle is empty when you access A for the first time (that is, when B has not returned the data, this is obviously troublesome and unwise.

 

Fortunately, startActivityForResult provides us with a better solution.

 

 

 

1. The startActivity (intent) method is not used during the jump, but startActivityForResult (intent, 0 ).

 

 

 

 

Java code

Intent intent = new Intent ();

Intent. setClass (A. this, B. class );

Bundle bundle = new Bundle ();

String str1 = "aaaaaa ";

Bundle. putString ("str1", str1 );

Intent. putExtras (bundle );

StartActivityForResult (intent, 0 );

/Here startActivityForResult is used for jump. Here 0 is a basis and other values can be written, but must be greater than or equal to 0.

 

Intent intent = new Intent ();

Intent. setClass (A. this, B. class );

Bundle bundle = new Bundle ();

String str1 = "aaaaaa ";

Bundle. putString ("str1", str1 );

Intent. putExtras (bundle );

StartActivityForResult (intent, 0 );

// StartActivityForResult is used for redirect. 0 here is a basis and other values can be written, but must be greater than or equal to 0.

 

2. Override the onActivityResult method to receive data returned by B.

 

 

Java code

Protected void onActivityResult (int requestCode, int resultCode, Intent data ){

Switch (resultCode) {// resultCode indicates the flag of the callback. In B, RESULT_ OK is returned.

Case RESULT_ OK:

Bundle B = data. getExtras (); // data is the Intent returned by B.

String str = B. getString ("str1"); // str indicates the return value.

Break;

Default:

Break;

}

}

 

Protected void onActivityResult (int requestCode, int resultCode, Intent data ){

Switch (resultCode) {// resultCode indicates the flag of the callback. In B, RESULT_ OK is returned.

Case RESULT_ OK:

Bundle B = data. getExtras (); // data is the Intent returned by B.

String str = B. getString ("str1"); // str indicates the return value.

Break;

Default:

Break;

}

}

 

 

 

3. Use the setResult method when returning data in B, and then call the finish method.

In this way, when the finish method is called in B, the onActivityResult method is automatically called when the method jumps to A to obtain the intent returned in B.

 

 

 

Java code

SetResult (RESULT_ OK, intent); // intent is the intent with Bundle sent by A. You can also define A new Bundle by yourself.

Finish (); // you must call the finish () method.

 

SetResult (RESULT_ OK, intent); // intent is the intent with Bundle sent by A. You can also define A new Bundle by yourself.

Finish (); // you must call the finish () method www.2cto.com.

 

In this way, when the finish method is called in B, the onActivityResult method is automatically called when the method jumps to A to obtain the intent returned in B.

From Dream Idears

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.