A summary of learning experience based on Android Startactivityforresult _android

Source: Internet
Author: User
From last night to now finally debugging through a startactivityforresult example, online or some say too complex, or vague, make me go a lot of detours, so write a piece of experience.
in a main interface (the main activity) can be connected to many different modules (sub-function), when the completion of the child module to return to the main interface, but also return some of the child modules completed data to the main event processing. Starting the main interface with StartActivity is a new intent instance, and the main interface of the access is not tuned out under the activity stack, and one of the biggest problems with this is that you can't go back to the original interface to provide data or services to the main interface. This time will use the Startactivityforresult!
Objective: Mainactivity.java is the main interface, Secondactivity.java is a child function module, to start from main Second,second received the data from main after the work, press the key OK will report the results to main, At the same time close yourself back to main.
Specific implementation:
Divided into four parts:
1, in the mainactivity inside set a button sendbuddle, send data to secondactivity, at the same time jump to second interface. Key to listen for code:
Copy Code code as follows:

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, receive the data from the intent in main.
Copy Code code as follows:

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_second);
OKButton = (Button) Findviewbyid (R.id.ok); Press this OK button and 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, the secondactivity in the implementation of the Monitor OK button, press to return to the mainactivity, at the same time close themselves, and send the mainactivity data. The OK button's listening code is as follows:
Copy Code code 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 down! Easy to write, Pit Dad's some tutorials, this piece didn't give inent bound
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. To mainactivity its Onactivityresult method.
Copy Code code as follows:

@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, and the second activity returned the data is:" +str,
Toast.length_short). Show ();
}
}

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

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.