Role of Requestcode and ResultCode in Startactivityforresult ()

Source: Internet
Author: User

Startactivityforresult () in
* Requestcode The request code, that is, call Startactivityforresult () pass the past value
* ResultCode result code, which is used to identify which new activity the returned data came from

I. Examples:

1. Data returned after the new open activity has been closed in activity
Startactivityforresult (first parameter, second parameter)
first parameter : Gets the data returned after the new open activity is closed
second parameter : for the request code, can be numbered according to business requirements

 PackageCom.ljq.activitys;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button; Public  class mainactivity extends Activity {    Private Final StaticString 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 (NewView.onclicklistener () { Public void OnClick(View v) {//Get the data returned after the new open activity is closed                //The second parameter is the request code, can be numbered according to business requirementsStartactivityforresult (NewIntent (mainactivity. This, Otheractivity.class),1);    }        }); }/** * In order to get the data returned, it must be overridden in the previous activity (referring to the Mainactivity Class) Onactivityresult method * Requestcode Request code, that is, call STARTACTIVITYF Orresult () passes past values * ResultCode result code, which is used to identify which new activity the returned data came from .    @Override    protected void Onactivityresult(intRequestcode,intResultCode, Intent data) {String result = Data.getextras (). GetString ("Result");//Get data returned after new activity is closedLOG.I (TAG, result); }}

2. Before the new activity is closed, you need to return data to the previous activity

 PackageCom.ljq.activitys;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.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 (NewView.onclicklistener () { Public void OnClick(View v) {//data is returned using intentIntent Intent =NewIntent ();//Deposit The return data in intentIntent.putextra ("Result","My name is Linjiqin");//Set return dataOtheractivity. This. Setresult (RESULT_OK, intent);//Close activityOtheractivity. This. Finish ();    }        }); }}the first parameter value of the//setresult () method can be defined by its own business needs
Two. function of the request code:
With Startactivityforresult (Intent Intent,intRequestcode) method to open a new activity, we need to pass in a request code (the second parameter) for the Startactivityforresult () method. The value of the request code is self-set according to the business need and is used to identify the source of the request. For example: An activity with two buttons, click on both buttons will open the same activity, whether it is the button to open the new activity, when the new activity is closed, the system will call the previous activity Onactivityresult (intRequestcode,intResultCode, Intent data) method. In the Onactivityresult () method, you can do this if you need to know that the new activity is opened by that button and you want to do the appropriate business processing:@Override   Public void onCreate(Bundle savedinstancestate) {..... button1.setonclicklistener (NewView.onclicklistener () { Public void OnClick(View v) {Startactivityforresult (NewIntent (mainactivity. This, Newactivity.class),1);        }        }); Button2.setonclicklistener (NewView.onclicklistener () { Public void OnClick(View v) {Startactivityforresult (NewIntent (mainactivity. This, Newactivity.class),2); }        });@Override protected void Onactivityresult(intRequestcode,intResultCode, Intent data) {Switch(Requestcode) { Case 1://Request from Button 1, for the corresponding business processing                    Case 2://Request from Button 2, for the corresponding business processing}          }}
Three. Effect of the result code
In an activity, you might use the Startactivityforresult () method to open several different activities to handle different businesses, and when these new activity is closed, The system invokes the previous activity's Onactivityresult (intRequestcode,intResultCode, Intent data) method. In order to know which new activity the returned data comes from, you can do so in the Onactivityresult () method (Resultactivity and newactivity are the new activity to open): 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 the activity will open resultactivity and newactivity       @Override protected void Onactivityresult(intRequestcode,intResultCode, Intent data) {Switch(ResultCode) { Case 1://resultactivity return Data                    Case 2://newactivity return Data}          }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Role of Requestcode and ResultCode in Startactivityforresult ()

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.