- hoạt động đầu tiên bắt đầu hoạt động seconds với startActivityForResult
- hoạt động thứ hai bắt đầu hoạt động thứ ba với startActivityForResult
Kết quả mong đợi: thứ ba (cuối cùng) hoạt động thiết lập một kết quả được đánh bắt trên giây thứ hai hoạt động của onActivityResult
Kết quả hiện tại: thứ ba (cuối cùng) hoạt động thiết lập một kết quả được ưa chuộng onActivityResult hoạt động đầu tiên của
Làm thế nào tôi có thể thiết lập một kết quả về các hoạt động thứ ba sẽ bị bắt vào thứ ba hoạt động thứ hai của (cuối cùng) hoạt động đặt ra một kết quả mà bị bắt gặp trên onActivityResult của hoạt động thứ hai?
Một số đoạn mã:
hoạt động đầu tiên
public class TestProjActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
Log.i("TAAAG", "1st activity - startActivityForResult");
Intent intent = new Intent(TestProjActivity.this, Activ2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivityForResult(intent, 1008);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("TAAAG", "1st activity - onActivityResult");
if (resultCode == RESULT_OK) {
Log.i("TAAAG", String.valueOf(requestCode));
switch (requestCode) {
case 1008:
String info = data.getExtras().getString("KEY1");
Log.i("TAAAG", "1st activity - onActivityResult - printing result");
Log.i("TAAAG", info);
break;
}
}
}
}
hoạt động Thứ hai
public class Activ2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("TAAAG", "2nd activity - startActivityForResult");
Intent intent = new Intent(Activ2.this, Activ3.class);
startActivityForResult(intent, 1009);
Intent intent2 = new Intent();
intent2.putExtra("KEY1", "VALUE1");
setResult(RESULT_OK, intent2);
finish();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("TAAAG", "2nd activity - onActivityResult");
if (resultCode == RESULT_OK) {
Log.i("TAAAG", String.valueOf(requestCode));
switch (requestCode) {
case 1009:
String info = data.getExtras().getString("KEY2");
Log.i("TAAAG", "2nd activity - onActivityResult - printing result");
Log.i("TAAAG", info);
break;
}
}
}
}
hoạt động thứ ba
public class Activ3 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("TAAAG", "3rd activity - Inserting Value and finishing");
Intent intent = new Intent();
intent.putExtra("KEY2", "VALUE2");
setResult(RESULT_OK, intent);
finish();
}
}
Bạn đang hoàn thành hoạt động thứ hai của mình j sau khi bạn khởi chạy chương trình thứ ba. Vì vậy, từ những gì tôi biết hoạt động đầu tiên sẽ nhận được kết quả kể từ khi hoạt động thứ hai không còn hoạt động nữa. Cố gắng loại bỏ kết thúc(); từ khi tạo trong Activty2 và xem điều gì xảy ra! –
Có, cảm ơn bạn - nó đã giải quyết được vấn đề! – Chaiavi