Transfer and return values for Android two activity

Source: Internet
Author: User

Mainactivity.java

Intent Intent = new Intent ();
Intent.putextra ("message", "the value passed to Secondactivity");
Intent.setclass (Mainactivity.this, Secondactivity.class);
/*
 * If you want to start another activity and want to have a return value, you need to use the Startactivityforresult method,
 * The first parameter is the intent object, and the second argument is a Requestcode value, if you have more than one button to start activity, requestcode marks the activity
 */
Startactivityforresult that each button launches (intent, 1000);


/**
 * The return value of all activity objects is received by this method
 * Requestcode: Represents a simultaneous value that initiates an activity requestcode past br>  * ResultCode: Represents the ResultCode value of the activated activity callback value
 * data: Represents the intent object returned by the startup activity
 */
@Override
protected void onactivityresult (int requestcode, int resultcode, Intent data)
{
Super . Onactivityresult (Requestcode, ResultCode, data);
if (Requestcode = = && ResultCode = = 1001)
{
String result_value = Data.getstringext RA ("result");
}
}


Secondactivity.java

Received in the OnCreate method, and can be received in other methods
String mainstring = Getintent.getstringextra ("message");


overriding return events
@Override
public boolean onKeyDown (int keycode, keyevent event) {
if (keycode = = Keyevent.keycode_back) {
Intent Intent = new Intent ();
Intent.putextra ("result", result);
/*
* Calling the Setresult method means I return the intent object to the previous activity so that the intent object can be obtained in the Onactivityresult method.
*/
Setresult (1001, intent);
return true;
}
Return Super.onkeydown (KeyCode, event);
}

Transfer and return values for Android two activity

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.