Objective: A. java is the main interface and B. java is A sub-function module. The result should be reported to A after B is started and B is finished.
First look at the relevant code of A. java
// -- A. java --//
/*
* Two things are required. The first is to start B with startActivityForResult (), and the second is to recycle B's results.
*/
// Start B
Java code
Intent bintent = new Intent (A. this, B. class );
// Set a bintent Bundle Value
String bsay = "Hello, this is B speaking ";
Bintent. putExtra ("listenB", bsay)
StartActivityForResult (bintent, 0); // The requestCode of the parameter (Intent intent, Int requestCode) corresponds to
// Rewrite onActivityResult () to process the returned data. It is recommended that you read the B. java code first and check it again.
// There are three parameters: requestCode, resultCode, and data.
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Switch (resultCode) {// resultCode indicates the flag of the callback. In B, RESULT_ OK is returned.
Case RESULT_ OK:
Bundle B = data. getExtras (); // data is the Intent returned by B.
String str = B. getString ("ListenB"); // str indicates the return value "Hello, this is B speaking"
/* What will be done after B returns the data... omitted */
Break;
Default:
Break;
}
}
Intent bintent = new Intent (A. this, B. class );
// Set a bintent Bundle Value
String bsay = "Hello, this is B speaking ";
Bintent. putExtra ("listenB", bsay)
StartActivityForResult (bintent, 0); // The requestCode of the parameter (Intent intent, Int requestCode) corresponds to
// Rewrite onActivityResult () to process the returned data. It is recommended that you read the B. java code first and check it again.
// There are three parameters: requestCode, resultCode, and data.
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Switch (resultCode) {// resultCode indicates the flag of the callback. In B, RESULT_ OK is returned.
Case RESULT_ OK:
Bundle B = data. getExtras (); // data is the Intent returned by B.
String str = B. getString ("ListenB"); // str indicates the return value "Hello, this is B speaking"
/* What will be done after B returns the data... omitted */
Break;
Default:
Break;
}
}
Bytes -------------------------------------------------------------------------------------
// -- B. java --//
// After you use setResut () to prepare the data to be returned, you only need to use the finish () method to send the packaged data to A and run the onActivityResult () part of the code.
Java code
Intent aintent = new Intent (B. this, A. class );
/* Process of packaging data to aintent Bundle */
SetResut (RESULT_ OK, aintent); // There are two parameters (int resultCode, Intent intent)
......
Finish ();
Intent aintent = new Intent (B. this, A. class );
/* Process of packaging data to aintent Bundle */
SetResut (RESULT_ OK, aintent); // There are two parameters (int resultCode, Intent intent)
......
Finish ();
Bytes -------------------------------------------------------------------------------------
OK. The Code is as above. There may be some questions about the parameters at this time. Let's look at the help of the android sdk to make it clearer. I have found some articles on the Internet that indicate that the confusion between requestCode and resultCode is incorrect.
StartActivityForResult (Intent intent, Int requestCode)
Intent passed to B. I don't want to explain it. If you don't understand it, you can play with your phone. Don't want to develop things.
RequestCode> = 0, which is used to distinguish the data returned by a submodule in onActivityResult. for java, D, and even E sub-modules, it is better to separate different requestcodes for each partition.
SetResut (int resultCode, Intent intent)
ResultCode if the submodule B may return several different results, you can use this parameter to identify and distinguish. There is also a special RESULT_ OK value here, it is good to use it without special circumstances, the sdk has instructions, ah.
Intent continues without explanation. onActivityResult () returned to ()
Www.2cto.com
OnActivityResult (int requestCode, int resultCode, Intent intent)
There is no need to explain the above three items. If you do not distinguish requestCode from resultCode, there will be no difference as long as other activities setResult reach A onActivityResult.
Conclusion: after converting Activity A to Activity B, expression B returns parameter A for acceptance. expression A determines the processing method based on requestCode IN THE onActivityResult method.
From Dream Idears