"Turn" Android Startactivityforresult learning Experience

Source: Internet
Author: User

http://blog.csdn.net/yanzi1225627/article/details/7800529

From last night to now finally debug through a startactivityforresult example, online either some said too complex, or said vague, make I go a lot of detours, so write a story.

In a main interface (main activity) can be connected to many different sub-function modules (child activity up), when the sub-module things are done back to the main interface, but also return some of the sub-module completed data to the main activity processing. Starting the main interface with StartActivity is a new intent instance, and the main interface of access is not tuned out below the activity stack, so one of the biggest problems is that it is not possible to return to the original interface without having to provide data or services to the main interface. This time will be used Startactivityforresult!

Purpose: Mainactivity.java is the main interface, Secondactivity.java is a sub-function module, to start from main Second,second receive the data from main after the work, press the button OK will report the results to main, Also close itself back to main.

Specific implementation:

Divided into four parts:

1, in the Mainactivity set a button sendbuddle, send data to secondactivity, while jumping to the second interface. Key Monitoring code:

Class Sendbuttonlisten implements onclicklistener{


public void OnClick (View v) {
TODO auto-generated Method Stub
Intent Intent = new Intent ();
String str = "Dajia Hao";
Intent.putextra ("send", str);
Intent.setclass (Mainactivity.this, Secondactivity.class);
Startactivityforresult (Intent, 0);
}

}

2, in the OnCreate function inside the secondactivity, receives the data from the intent in main.

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_second);
OKButton = (Button) Findviewbyid (R.id.ok); Pressing this OK key will return to main.
Intent Intent = Getintent ();
String getstr = Intent.getstringextra ("send");
TextView TV = (TextView) Findviewbyid (R.id.sendtext);
Tv.settext (GETSTR);
Toast.maketext (Secondactivity.this,
"The data sent back from Mainactivity is:" +getstr,
Toast.length_short). Show ();
Okbutton.setonclicklistener (New Okbuttonlisten ());


}

3, in the secondactivity to implement the Monitoring OK button, press the back to mainactivity, while self-closing, and send data to mainactivity. The listening code for the OK button is as follows:

Class Okbuttonlisten implements onclicklistener{


public void OnClick (View v) {
TODO auto-generated Method Stub
Intent sendintent = new Intent (secondactivity.this, Mainactivity.class); //This method is learned today, write it down! Easy to write, some of the pit daddy tutorials, this piece does not bind to inent
Bundle bundle = new bundle ();
Bundle.putstring ("Send", "everyone good");
Sendintent.putextras (bundle);
SecondActivity.this.setResult (RESULT_OK, sendintent);
SecondActivity.this.finish ();

}


4, after returning to main, main to receive the data sent by second. The Onactivityresult method of copying it in mainactivity.

@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
TODO auto-generated Method Stub
Super.onactivityresult (Requestcode, ResultCode, data);
if (RESULTCODE==RESULT_OK) {
Bundle bundle = Data.getextras ();
String str = bundle.getstring ("send");
Toast.maketext (Mainactivity.this,
"I came back, the second activity returned the data is:" +str,
Toast.length_short). Show ();

}
}

Note: Do not create a new Intent,onactivityresult there are three parameters, the third parameter is intent, just need to use him to do the parameters on the line.

Write should be quite clear, not clear the message asked me! Learn together.

"Turn" Android Startactivityforresult learning Experience

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.