The difference between startActivityForResult and startActivity is:
1. startActivity( )
just jumps to the target page. If you want to jump back to the current page, you must use startActivity( ) again.
2. startActivityForResult( )
can complete this task at one time. When the program executes this code, if it jumps from T1Activity to the next Text2Activity, and when the Text2Activity calls the finish() method, the program will automatically jump Go back to T1Activity and call the onActivityResult( ) method in the previous T1Activity.
相关函数:
startActivityForResult(Intent intent, Int requestCode)
setResut(int resultCode, Intent intent)
onActivityResult(int requestCode, int resultCode, Intent intent)
Simple example introduction:
1. When jumping, instead of using the startActivity(intent) method, startActivityForResult(intent, 0
)
Intent intent=new Intent();
intent.setClass(A.this, B.class);
Bundle bundle=new Bundle();
String str1=”aaaaaa”;
bundle.putString(“str1”, str1);
intent.putExtras (bundle);
startActivityForResult(intent, 0);//startActivityForResult is used to jump here, 0 here is a basis, other values can be written, but must be >=0
copy code
2. Rewrite the onActivityResult method to receive the data returned by B.
copy code
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) { //resultCode is the returned mark, what I return in B is RESULT_OK
case RESULT_OK:
Bundle b=data.getExtras(); / /data is the Intent
String returned in B str=b.getString(“str1”);//str is the returned value
break;
default:
break;
}
}
copy code
3. Use the setResult method when returning data in B, and then call the finish method.
setResult(RESULT_OK, intent); //intent is the intent with Bundle from A, of course, you can also define a new Bundle
finish();//The finish() method must be called here
When is the setResult() of the Android activity called (the point is also the difficulty)
If setResult is set in the Activity from startActivityForResult, the result will not be returned to the parent's Activity immediately. Only when the current Activity is finished, the result will be sent to the parent's onActivityResult for processing!
If an activity wants to return data to the activity that started it, it can call the setResult() method. When to call the setResult() method to return data?
Take a look at the source code to understand:
copy code
public final void setResult(int resultCode, Intent data) {
synchronized (this) {
mResultCode = resultCode;
mResultData = data;
}
}
public void finish() {
if (mParent == null) {
int resultCode;
Intent resultData;
synchronized (this) {
resultCode = mResultCode;
resultData = mResultData;
}
if (Config.LOGV) Log.v(TAG, "Finishing self: token=" + mToken);
try {
if (ActivityManagerNative.getDefault()
.finishActivity(mToken, resultCode, resultData)) {
mFinished = true;
}
} catch (RemoteException e) {
// Empty
}
} else {
mParent.finishFromChild(this);
}
}
copy code
This code shows that the activity returns the result when it is finished, that is to say, the call to the setResult() method must be done before finish().
Then if you call setResult() in the following methods, it may not return success: onPause(), onStop(), onDestroy(),
because these method calls are not necessarily before finish, of course, calling setResult in onCreate() is sure before finish
Press the BACK key to exit from an Activity. Once you press BACK, android will automatically call the Activity's finish() method, and then set the resultCode to RESULT_CANCELED, and no data will be returned. The
solution is to capture and press BACK in the Activity. After capturing the event, setResult first, and then call finish, and you're done... Swallow the BACK event directly by yourself
@Override
public void onBackPressed() {
Log.i(TAG, “onBackPressed”);
setResult(Const.LIVE_OK);
super.onBackPressed();
}
Of course, you can also call setResult in onCreate(), but I don't think this method is better than overriding the onBackPressed() method.