Onactivityresult () in Android gets the return value

Source: Internet
Author: User

Requirements: Jumps from firstactivity to Secondactivity, operates in secondactivity, and returns to Firstactivity.

The main code in firstactivity:

private static final int request_code = 1;

private static final int result_code = 101;

Triggering events

public void Gotosencond () {

Intent intent=new Intent (this,secondactivity.class);

Startactivityforresult (Intent, Request_code);

}

The second parameter in the Onactivityresult () method is the key returned by the secondactivity, and the third parameter is the value of the key that Secondactivity returns.

@Override

protected void Onactivityresult (int requestcode, int resultcode, Intent data) {

if (Requestcode==request_code) {

if (Resultcode==result_code) {

String Result=data.getstringextra ("second");//Gets the contents of the secondactivity returned

Textview.settext (result);

}

}

Super.onactivityresult (Requestcode, ResultCode, data);

}

Code in the Secondactivity class:

  

final int result_code=101;

public void CloseButton (view view) {

Intent intent=new Intent ();

Intent.putextra ("Second", "I am second!");

Setresult (Result_code, intent);

Finish ();

}

Onactivityresult () in Android gets the return value

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.