The jump between androidactivities is not only for startActivity (Intent I), but also for startActivityForResult (Intent intent, int requestCode.
The onActivityResult (int requestCode, int resultCode, Intent data) method can be used to obtain the operation after the end of the Request Activity.
Note three methods: startActivityForResult (Intent intent, int requestCode), onActivityResult (int requestCode, int resultCode, Intent data), setResult (int resultCode, Intent data)
For example, the following code redirects From to ToB and ToC.
From:
If (condition ){
Intent intent = new Intent (this, ToB. class );
StartActivityForResult (serverIntent, REQUEST_CODE_01); // jump to ToB
} Else {
Intent intent = new Intent (this, ToC. class );
StartActivityForResult (serverIntent, REQUEST_CODE_02); // jump to ToC
}
Public void onActivityResult (int requestCode, int resultCode, Intent data ){
Switch (requestCode ){
Case REQUEST_CODE_01:
If (resultCode = Activity. RESULT_ OK)
// Do something
Break;
Case REQUEST_CODE_02:
// Do something
Break;
}
}
Here we will talk about the startActivityForResult (Intent intent, int requestCode) parameter. Needless to say, the first Intent is the second one. We use REQUEST_CODE_01 and REQUEST_CODE_02, in fact, this is an int type constant we have defined for marking. The specific function can be seen in the onActivityResult method to determine which Activity is returned from.
ToB:
Intent intent = new Intent ();
Intent. putExtra (key, value );
SetResult (Activity. RESULT_ OK, intent );
Finish (); // The result is returned to the From
ToC:
Intent intent = new Intent ();
Intent. putExtra (key, value );
SetResult (Activity. RESULT_ OK, intent );
Finish (); // The result is returned to the From
The first parameter of setResult corresponds to the second parameter of onActivityResult above. Do not confuse the first parameter of onActivityResult with the second parameter. One is the request tag and the other is the return tag.
Please add ~~~
From TQUDING's column