StartActivity can start a new activity, which is relatively independent of the activity that initiates it, and does not return to the result of the activity that invoked it. When writing applications, it is sometimes necessary to return results from other activity. Receive result shows this usage.
Startactivityforresult use methods similar to startactivity, it is important that Startactivityforresult can return results from the invoked activity. Get_code is the request code, which can be used to handle the results returned by the activity according to the request code when the child activity returns the result.
Definition of the one requestcode we use for receiving resuls.
Static final private int get_code = 0;
Private Onclicklistener Mgetlistener = new Onclicklistener () {public
void OnClick (View v.) {
//Start the activity whose result we want to retrieve. The
//result would come back with request code Get_code.
Intent Intent = new Intent (receiveresult.this, sendresult.class);
Startactivityforresult (Intent, Get_code);
}
;
When the child activity ends, call Setresult to set the return result. The Receive result example involves two activity, Receiveresult for the parent Activity,sendresult returns the user option for the child activity, and the results returned are displayed in Receiveresult.
Sendresult returns the following code for the result:
Private Onclicklistener Mcorkylistener = new Onclicklistener ()
{public
void OnClick (View v)
{
//To Send a result, simply call Setresult () before your
//the activity is finished.
Setresult (RESULT_OK, (New Intent ()). Setaction ("corky!"));
Finish ();
}
;
Private Onclicklistener Mvioletlistener = new Onclicklistener ()
{public
void OnClick (View v)
{
// To send a result, the Simply call Setresult () before your//the activity is
finished.
Setresult (RESULT_OK, (New Intent ()). Setaction ("violet!"));
Finish ();
}
;
When the result is returned from a child activity, the parent activity processes the results returned by the child activity by defining Onactivityresult.
protected void Onactivityresult (int requestcode, int resultcode,
Intent data) {
//can use the Requestcode to Select between multiple child
//Activities your may have started. Here there are only one thing
//We launch.
if (Requestcode = = Get_code) {
//We'll be adding to our text.
...
}
}