Android startActivityForResult usage

Source: Internet
Author: User

StartActivity (Intent) is used to redirect activities)
But what if the following conditions are met?
Activity1 jumps to Activity2, but does it need to return to Activity1 in Activity2? Some people may say: I can use startActivity () again in Activity2. Yes, but startActivityForResult () can do this directly.
[Example]
Activity1: there are two EditText strings used to receive user input. The two strings need to be connected. I now hand over the connection work to Activity2 and return the connected string to Activity1. and it is responsible for displaying
[Code]
1. Build the interface required by Activity1
Java code

Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<EditText
Android: id = "@ + id/first"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
<EditText
Android: id = "@ + id/second"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
<Button
Android: id = "@ + id/start"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "start"
/>
<TextView
Android: id = "@ + id/text"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "... is waiting"
/>
</LinearLayout>

2. Get two EditText user input.

Copy codeThe Code is as follows: first = (EditText) findViewById (R. id. first );
Second = (EditText) findViewById (R. id. second );

3. Load the string into the Bundle, place it in the Intent, and then send it.

Copy codeThe Code is as follows: Intent I = new Intent (this, Activity2.class );

Bundle B = new Bundle ();

B. putString ("first", first. getText (). toString ());
B. putString ("second", second. getText (). toString ());

I. putExtras (B );

StartActivityForResult (I, 10 );

Supplement:

Copy codeThe Code is as follows: public void startActivityForResult (Intent intent, int requestCode)

Intent intent: the system will determine the target Activity based on this

Int requestCode: used to identify whether the Intent is returned or not.

4. register the View listener

Copy codeThe Code is as follows: findViewById (R. id. start). setOnClickListener (new OnClickListener (){
Public void onClick (View v ){
// TODO Auto-generated method stub
SendCalculate ();
}
});

5. Construct the Activity2 interface to return the processing result

Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Button
Android: id = "@ + id/reply"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "reply"
/>
</LinearLayout>

6. Get the passed Intent and the passed two strings and connect them.

Copy codeThe Code is as follows: Intent I = this. getIntent ();

Bundle B = I. getExtras ();

String v1 = B. getString ("first ");
String v2 = B. getString ("second ");

Value = v1 + v2;

7. Define Intent and store the returned results and return them

Copy codeThe Code is as follows: Intent I = new Intent ();

Bundle B = new Bundle ();
B. putString ("CALCULATION", value );

I. putExtras (B );

This. setResult (RESULT_ OK, I );
This. finish ();

8. Is the task finished? Of course not, do not forget that Activity1 still needs to receive data and display it.

Copy codeThe Code is as follows: protected void onActivityResult (int requestCode, int resultCode,
Intent data ){
Switch (resultCode ){
Case RESULT_ OK:
Bundle B = data. getExtras ();

String string = B. getString ("CALCULATION ");

UpdateText (string );
}
}

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