Use of startactivityforresult in Android

Source: Internet
Author: User

1. If you want to get the data returned after the activity is closed, you must use the startactivityforresult (intent, int requestcode) method provided by the system to open the new activity, after the new activity is disabled, data is returned to the previous activity. To obtain the data returned, the onactivityresult (INT requestcode, int resultcode, intent data) method must be rewritten in the previous activity.

Package COM. ljq. activitys; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. util. log; import android. view. view; import android. widget. button; public class mainactivity extends activity {private final static string tag = "mainactivity"; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button btnopen = (button) This. findviewbyid (R. id. btnopen); btnopen. setonclicklistener (new view. onclicklistener () {public void onclick (view v) {// get the data returned after the activity is closed. // The second parameter is the request code, you can enter startactivityforresult (new intent (mainactivity. this, otheractivity. class), 1) ;}}) ;}/ *** to obtain the returned data, the onactivityresult method must be rewritten in the previous activity (mainactivity class) ** requestcode request code, that is, call startactivityforresult () to pass the previous value * resultcode result code. The result code is used to identify the new activity from which the returned data comes */@ override protected void onactivityresult (INT requestcode, int resultcode, intent data) {string result = data. getextras (). getstring ("result"); // get the data log returned after the new activity is disabled. I (TAG, result );}}

When the new activity is disabled, the data returned by the new activity is transmitted through intent. The Android platform calls the onactivityresult () method of the previous activity, input the intent that stores the returned data as the third input parameter, and use the third input parameter in the onactivityresult () method to retrieve the data returned by the new activity.

2. Use startactivityforresult (intent, int requestcode) to open a new activity. Before closing a new activity, you must use the setresult (INT resultcode, intent data) provided by the system to return data to the previous activity) method implementation:

Package COM. ljq. activitys; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. button; public class otheractivity extends activity {@ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. other); button btnclose = (button) findviewbyid (R. id. btnclose); btnclose. setonclicklistener (new view. onclicklistener () {public void onclick (view v) {// The data is returned using intent = new intent (); // Save the returned data to intent. putextra ("result", "My name is linjiqin"); // sets the returned data to otheractivity. this. setresult (result_ OK, intent); // close activity otheractivity. this. finish ();}});}}

The first parameter value of the setresult () method can be defined according to business needs. The result_ OK used in the code above is a constant defined by the system activity class. The value is-1, the code snippet is as follows:
Public class android. App. Activity extends ......{
Public static final int result_canceled = 0;
Public static final int result_ OK =-1;
Public static final int result_first_user = 1;
}

Running result

Note: When you click "open new activity", the "I am a new activity" page is displayed;

Click "close" to close the current page. At the same time, the "I am an old activity" page is displayed, and the result parameter is passed to the previous activity.

Role of the Request Code

Use the startactivityforresult (intent, int requestcode) method to open a new activity. We need to input a Request Code (the second parameter) for the startactivityforresult () method ). The value of the Request Code is set by the user according to the business needs, used to identify the request source. For example, a single activity has two buttons. Clicking these two buttons will open the same activity. No matter which button opens the new activity, when the new activity is closed, the system will call the onactivityresult (int
Requestcode, int resultcode, intent data) method. In the onactivityresult () method, if you need to know that the new activity is opened by that button and want to handle the corresponding business, you can do this:
@ Override public void oncreate (bundle savedinstancestate ){
....
Button1.setonclicklistener (New View. onclicklistener (){
Public void onclick (view v ){
Startactivityforresult (new intent (mainactivity. This, newactivity. Class), 1 );

}

});
Button2.setonclicklistener (New View. onclicklistener (){
Public void onclick (view v ){
Startactivityforresult (new intent (mainactivity. This, newactivity. Class), 2 );

}

});


@ Override protected void onactivityresult (INT requestcode, int resultcode, intent data ){
Switch (requestcode ){
Case 1:
// Request from button 1 for corresponding business processing
Case 2:
// Request from button 2 for corresponding business processing
}
}
}

Result code

In an activity, you may use the startactivityforresult () method to open multiple different activities to process different businesses. When these new activities are closed, the system will call the onactivityresult (INT requestcode, int resultcode, intent data) method of the previous activity. To know which new activity the returned data is from, you can do this in the onactivityresult () method (resultactivity and newactivity are the new activity to be opened ):
Public class resultactivity extends activity {
.....
Resultactivity. This. setresult (1, intent );
Resultactivity. This. Finish ();
}
Public class newactivity extends activity {
......
Newactivity. This. setresult (2, intent );
Newactivity. This. Finish ();
}
Public class mainactivity extends activity {// in this activity, the resultactivity and newactivity will be enabled
@ Override protected void onactivityresult (INT requestcode, int resultcode, intent data ){
Switch (resultcode ){
Case 1:
// Resultactivity returned data
Case 2:
// Newactivity returned data
}
}
}

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.