The Activity started in startActivity mode has nothing to do with its parent Activity. When it is disabled, no feedback is provided.
Flexible. You can start an Activity as a child Activity, which has an internal connection with the parent Activity. When a child Activity is disabled, it triggers an event processing function in the parent Activity. A child Activity is most suitable for an Activity that provides data for other activities (for example, a user selects a project from a list.
The creation of a sub-Activity is the same as that of a normal Activity, and must also be registered in the manifest of the application. Any Activity registered in manifest can be used as a sub-Activity.
Promoter Activity
The startActivityForResult method is similar to the startActivity method, but there is an important difference. Intent is used to determine which Activity to start. You can also input a request code. This value is used later as the unique ID of a returned Activity.
The following code shows how to start a child Activity:
Private static final int SHOW_SUBACTIVITY = 1;
Intent intent = new Intent (this, mytheractivity. class );
StartActivityForResult (intent, SHOW_SUBACTIVITY );
Like a normal Activity, a child Activity can be started implicitly or explicitly. The following framework code uses an implicit Intent to start a new subactivity to select a contact:
Private static final int PICK_CONTACT_SUBACTIVITY = 2;
Uri uri = Uri. parse ("content: // contacts/people ");
Intent intent = new Intent (Intent. ACTION_PICK, uri );
StartActivityForResult (intent, PICK_CONTACT_SUBACTIVITY );
Return Value
When the child Activity is about to be closed, call setResult Before finish to return a result for the called Activity.
The setResult method has two parameters: the result code and the load value expressed as Intent. The result code is the result of running a sub-Activity, generally Activity. RESULT_ OK or Activity. RESULT_CANCELED. In some cases, you may want to use your own response code to process the selection of a specific application. setResult supports any integer.
The Intent returned as a result can contain a URI pointing to a content (such as a contact, phone number, or media file) and a set of Extra for returning additional information.
The following code snippet is excerpted from the onCreate method of the sub-Activity and shows how to return different results to the called Activity:
Button okButton = (Button) findViewById (R. id. OK _button );
OkButton. setOnClickListener (new View. OnClickListener (){
Public void onClick (View view)
{
Uri data = Uri. parse ("content: // horses/" + selected_horse_id );
Intent result = new Intent (null, data );
Result. putExtra (IS_INPUT_CORRECT, inputCorrect );
Result. putExtra (SELECTED_PISTOL, selectedPistol );
SetResult (RESULT_ OK, result );
Finish ();
}
});
Button cancelButton = (Button) findViewById (R. id. cancel_button );
CancelButton. setOnClickListener (new View. OnClickListener (){
Public void onClick (View view)
{
SetResult (RESULT_CANCELED, null );
Finish ();
}
});
Processing the results of a sub-Activity
When a child Activity is disabled, the onActivityResult event processing function of its parent Activity is triggered.
Override this method to process the results returned from the Child Activity. The onActivityResult processor accepts several parameters:
? Request Code
The request code used to start the Activity.
? Result code
The result code is set by a child Activity to display its results. It can be any integer, but the typical values are Activity. RESULT_ OK and Activity. RESULT_CANCELLED.
If the sub-Activity is disabled abnormally or the result code is not specified when it is disabled, the result code is Activity. RESULT_CANCELED.
? Data
One Intent to package any returned data. Depending on the purpose of the sub-Activity, it may contain a URI representing the special data selected from the list. The child Activity can use the "extras" mechanism to return temporary information in the form of basic values.
The following framework code implements the onActivityResult event processing function in an Activity:
Private static final int SHOW_SUB_ACTIVITY_ONE = 1;
Private static final int SHOW_SUB_ACTIVITY_TWO = 2;
@ Override
Public void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
Switch (requestCode)
{
Case (SHOW_SUB_ACTIVITY_ONE ):
{
If (resultCode = Activity. RESULT_ OK)
{
Uri horse = data. getData ();
Boolean inputCorrect = data. getBooleanExtra (IS_INPUT_CORRECT, false );
String selectedPistol = data. getStringExtra (SELECTED_PISTOL );
}
Break;
}
Case (SHOW_SUB_ACTIVITY_TWO ):
{
If (resultCode = Activity. RESULT_ OK)
{
// TODO: Handle OK click.
}
Break;
}
}
}
Excerpt from advancing with the times