看來你還沒有正確理解這個概念!這可能會幫助您瞭解onActivityResult。
通過使用startActivityForResult(Intent intent, int requestCode)
可以開始另一個活動,然後收到結果從onActivityResult()
method.So onActivityResult()
該活動是從那裏開始的另一個活動。
onActivityResult(int requestCode, int resultCode, Intent data)
檢查這裏的參數。請求代碼在那裏從您得到結果的地方進行過濾。所以你可以使用他們的requestCodes來識別不同的數據!
例
public class MainActivity extends Activity {
// Use a unique request code for each use case
private static final int REQUEST_CODE_EXAMPLE = 0x9988;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an Intent to start AnotherActivity
final Intent intent = new Intent(this, AnotherActivity.class);
// Start AnotherActivity with the request code
startActivityForResult(intent, REQUEST_CODE_EXAMPLE);
}
//-------- When a result is returned from another Activity onActivityResult is called.--------- //
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// First we need to check if the requestCode matches the one we used.
if(requestCode == REQUEST_CODE_EXAMPLE) {
// The resultCode is set by the AnotherActivity
// By convention RESULT_OK means that what ever
// AnotherActivity did was successful
if(resultCode == Activity.RESULT_OK) {
// Get the result from the returned Intent
final String result = data.getStringExtra(AnotherActivity.EXTRA_DATA);
// Use the data - in this case, display it in a Toast.
Toast.makeText(this, "Result: " + result, Toast.LENGTH_LONG).show();
} else {
// AnotherActivity was not successful. No data to retrieve.
}
}
}
}
AnotherActivity
< - 這是我們用來發送數據到MainActivity
public class AnotherActivity extends Activity {
// Constant used to identify data sent between Activities.
public static final String EXTRA_DATA = "EXTRA_DATA";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
final View button = findViewById(R.id.button);
// When this button is clicked we want to return a result
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Create a new Intent as container for the result
final Intent data = new Intent();
// Add the required data to be returned to the MainActivity
data.putExtra(EXTRA_DATA, "Some interesting data!");
// Set the resultCode to Activity.RESULT_OK to
// indicate a success and attach the Intent
// which contains our result data
setResult(Activity.RESULT_OK, data);
// With finish() we close the AnotherActivity to
// return to MainActivity
finish();
}
});
}
@Override
public void onBackPressed() {
// When the user hits the back button set the resultCode
// to Activity.RESULT_CANCELED to indicate a failure
setResult(Activity.RESULT_CANCELED);
super.onBackPressed();
}
}
注意之一:在MainActivity
現在檢查你startActivityForResult
有您指定的REQUEST_CODE
。假設您想要撥打三種不同的活動來獲得結果..所以有三個startActivityForResult
來電有三個不同的REQUEST_CODE's。 REQUEST_CODE只不過是您在活動中指定的唯一密鑰,用於唯一標識您的startActivityForResult
調用。
一旦您從這些活動接收到數據,您可以檢查什麼是REQUEST_CODE,那麼你知道啊哈,這個結果來自於這個活動。
這就像你發送郵件給你的愛人與豐富多彩的封面,並要求他們在同一封面答覆。那麼如果你從他們那裏收到一封信,你知道是誰給你寄了一封。 awww;)
簡單地檢查onActivityResult方法中requestCode的價值,你是好去。請求代碼可以在使用intent –
調用setResult()的活動時爲這兩個活動設置不同的值。 finish();'不關心Intent參數的名字是什麼 –