onActivityResultを理解するのに役立ちます。あなたは別のアクティビティを開始することができ、その後、がonActivityResult()
method.So onActivityResult()
でその活動から結果を受け取るstartActivityForResult(Intent intent, int requestCode)
を使用することにより
は、別のアクティビティを開始するところからです。
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();
}
}
にデータを送信するために使用するもの注:今、あなたはREQUEST_CODE
を指定そこstartActivityForResult
MainActivity
であなたをチェックします。結果を得るために3つの異なるアクティビティを呼び出したいとします。REQUEST_CODEの3つのstartActivityForResult
コールが3つあります。 REQUEST_CODEは、アクティビティで指定した一意のキーだけで、startActivityForResult
の呼び出しを一意に識別します。
これらのアクティビティからデータを受け取ったら、REQUEST_CODEが何であるかを確認できます。この結果はこのアクティビティからのものです。
あなたはカラフルなカバーであなたの恋人にメールを送って、同じカバーで返信するようお願いします。その後、あなたから手紙を受け取ったら、誰があなたのためにそれを送ったかを知っています。 awww;)