あなたは概念を正しく理解していないようですね!これは、startActivityForResultを理解するのに役立ちます。あなたは別のアクティビティを開始することができ、その後、がonActivityResult()
method.So onActivityResult()
でその活動から結果を受け取るstartActivityForResult(Intent intent, int requestCode)
を使用することにより
は、別のアクティビティを開始するところからです。
例
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();
}
}
そして、あなたのタスクのためにあなたはそれがあります上記の方法を必要としませんにデータを送信するために使用するものあなたの知識のために、以下のようにしてください
<Button
android:onClick="gatherResults" <--- method name in your class
android:id="@+id/button_id_one"
android:layout_width="match_parent"
android:layout_height="100dp" />
あなたの好きなボタンをお持ちください!そして彼らはあなたがの意図に値を入れている
public void gatherResults(View view){
// Perform action on click
switch(view.getId()) {
case R.id.button_id_one:
//DO what you want for specific id button_id_one
break;
case R.id.button_id_two:
//DO what you want specific id button_id_two
break;
}
}
は*です:-)お手伝いを致します*置く**オブジェクトまたは文字列? –
場所は文字列です – user7327850
あなたはonActivityResult内で取得しているインテントデータが異なります –