There are three Activity:a.java, B.java, C.java
Common ways to jump between activity:
1. StartActivity (Intent Intent);
This method is only used to start a new activity
2.startActivityForResult (Intent Intent, int requestcode);
The method does two things: one. Start a new activity two. Recycle the results returned by the activated activity
(1) in A.java
Startactivityforresult (Intent Intent, int requestcode);
Requestcode: is a custom int type constant, Requestcode >=0.
Cases:
| 1234567 |
if(条件){ Intent intent = newIntent(this, B.class); startActivityForResult(intent, REQUEST_CODE_01);//跳转至B.java}else{ Intent intent = newIntent(this, C.class); startActivityForResult(intent, REQUEST_CODE_02);//跳转至C.java} |
(2) B.java or C.java
Setresut (int resultcode, Intent Intent)
ResultCode: Used to identify several different return results for B.java. When there is no special case, use RESULT_OK.
Cases:
| 123 |
Intent intent = newIntent();intent.putExtra(key, value); // 或者将数据打包到BundlesetResult(RESULT_OK, intent); |
(3) A.java
Onactivityresult (int requestcode, int resultcode, Intent data)
Requestcode: The data used to distinguish which submodule is being returned.
Data: the postback.
Bundle B = Data.getextras ();
String str = b.getstring ("string");
ResultCode: Corresponds to Setresult ()
Cases:
| 1234567891011 |
publicvoidonActivityResult(intrequestCode, intresultCode, Intent data){ switch (requestCode) {caseREQUEST_CODE_01: if(resultCode==Activity.RESULT_OK) //do something break;caseREQUEST_CODE_02: //do something break;}} |
Android startactivity () and Onactivityresult () Usage Summary (reprint)