Android Activity switching and Activity data interaction, androidactivity
Within the Android program, startActivity starts a sub-Activity with Intent (expressed in a parent-child relationship, which is clear only and does not have the concept of Parent-Child Activity in Android ). As follows:
Intent intent = new Intent(LoginActivity.this, MainActivity.class);startActivity(intent);
Note:
1. LoginActivity. this indicates the Intent context.
2. MainActivity. class indicates the class of the child Activity to be started.
3. In addition to starting Activity, Intent can also be used to start Service and other apps.
4. After startActivity (intent) is executed, the current Activity overwrites the quilt Activity and pauses it. When the child Activity is disabled, the parent Activity that was previously overwritten resumes execution.
When the Parent and Child activities alternate (when the promoter Activity or the parent Activity resumes running), Android allows data interaction between two activities.
1. pass data to the newly started Child Activity
If you want to transmit data to a newly started sub-Activity, you only need to add key-value to the Intent object. key indicates the name of the character indicating the data to be transmitted, and value indicates the data to be transmitted.
intent.putExtra(“KEY",data);
startActivity(intent);
Obtain the passed data in the sub-Activity as follows:
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); DataType data= (DataType)getIntent().getDataTypeExtra(”KEY");}
Note:
1. Call in onCreate
2. DataType should be replaced with the data type you need
Certificate ------------------------------------------------------------------------------------------------------------------------------------------
2. return data from a Child Activity
To return data in a child Activity, you only need to add key-value to the Intent object before the child Activity ends, as shown below:
Intent intent = new Intent();
intent.putExtra(“KEY”, data);setResult(RESULT_OK, intent);this.finish();
Note:
1. setResult (RESULT_ OK, intent) sets the Child Activity to return the information of the parent Activity (the Activity that opens the new Activity). The RESULT_ OK value is int, and the table child Activity is successfully executed, intent contains the data to be returned.
2. Remember to close it. In the parent Activity, if you want to receive the returned results of the Child Activity, use startActivityForResult (intent, requestCode) to open the Child Activity and override the onActivityResult method.
The code for opening a Child Activity and expecting to return results is as follows:
............
Intent intent = new Intent(this, DrugLibActivity.class);startActivityForResult(intent, 10);
............@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { if (10 == requestCode) { int index = data.getIntExtra(KEY,0); }
}
Note:
1. The requestCode in startActivityForResult should be a value greater than or equal to 0 (it is recommended to use constants instead of more intuitive)
2. onActivityResult (int requestCode, int resultCode, Intent data) after the sub-Activtiy is disabled, requestCode is the requestCode value when the sub-Activity is started, and resultCode is the value set in the sub-Activity setResult.
3. If you can call startActivityForResult to open multiple sub-activities in multiple places in the same Activity, you should judge requestCode when onActivityResult is returned, in this way, you can find out which sub-Activity has returned data.
4. If AndroidManifest. xml is used, the child Activity that you want to open and accept the returned value is set to android: launchMode = "singleTask ". Sorry, onActivityResult will be executed immediately after startActivityForResult is called, and the subform has not yet returned.
Why? First, we need to figure out what launchMode = "singleTask" is ".