[PS: Or take Google's native bluetoothchat as an example]
1. The difference between startactivity and startactivityforresult
StartActivity () simply jumps to the target page, and if you want to jump back to the current page, you must use StartActivity () again.
Startactivityforresult () can complete this task at once, and when the program executes to this code, if it jumps from t1activity to the next text2activity,
When this text2activity invokes the finish () method, the program automatically jumps back to t1activity and calls the Onactivityresult () method in the previous t1activity.
For example, in the Bluetoothchat class, The Startactivityforresult () method is used to jump to the Devicelistactivity class:
@Override Public Booleanonoptionsitemselected (MenuItem item) {Intent serverintent=NULL; Switch(Item.getitemid ()) { CaseR.id.secure_connect_scan://Launch the devicelistactivity to see Devices and do scanServerintent =NewIntent ( This,devicelistactivity.class); Startactivityforresult (Serverintent, request_connect_device_secure); return true; CaseR.id.insecure_connect_scan://Launch the devicelistactivity to see Devices and do scanServerintent =NewIntent ( This, Devicelistactivity.class); Startactivityforresult (Serverintent, request_connect_device_insecure); return true; Caser.id.discoverable://ensure this device was discoverable by othersensurediscoverable (); return true; } return false; }
In Deviceactivity, the value is returned using the Setresult () method:
//The On-click listener for all devices in the ListViews PrivateOnitemclicklistener Mdeviceclicklistener =NewOnitemclicklistener () { Public voidOnitemclick (adapterview<?> av, View V,intArg2,LongArg3) { //Cancel Discovery because it ' s costly and we ' re about to connectMbtadapter.canceldiscovery (); //Get The device MAC address, which is the last chars in the ViewString info =((TextView) v). GetText (). toString (); String Address= Info.substring (Info.length ()-17); //Create The result Intent and include the MAC addressIntent Intent =NewIntent (); Intent.putextra (extra_device_address, ADDRESS); //Set result and finish this Activity Setresult (ACTIVITY.RESULT_OK, intent); Finish (); } };
However, be aware that if you set setresult in the activity that Startactivityforresult jumps to, the result will not be returned immediately to the activity of the parent, only the current activity is finish,
The result will be sent to the parent's Onactivityresult to handle:
Public voidOnactivityresult (intRequestcode,intResultCode, Intent data) { if(D) log.d (TAG, "Onactivityresult" +ResultCode); Switch(requestcode) { Caserequest_connect_device_secure://When devicelistactivity returns with a device to connect if(ResultCode = = ACTIVITY.RESULT_OK) {Connectdevice (data,true); } Break; Caserequest_connect_device_insecure://When devicelistactivity returns with a device to connect if(ResultCode = =ACTIVITY.RESULT_OK) {Connectdevice (data,false); } Break; CaseREQUEST_ENABLE_BT://When the request to enable Bluetooth returns if(ResultCode = =ACTIVITY.RESULT_OK) { //Bluetooth is now enabled, so set up a chat sessionSetupchat (); } Else { //User did not enable Bluetooth or an error occurredLOG.D (TAG, "BT not enabled"); Toast.maketext ( This, R.string.bt_not_enabled_leaving, Toast.length_short). Show (); Finish (); } } }
Android StartActivity, Startactivityforresult and Setresult