我需要你的帮助:我希望在完成当前活动之前将以前的活动数据添加到上一个活动中。
例如:活动A开始活动B. 当我完成活动B时,我想要活动A新数据。
我怎么能这样做? 非常感谢
答案 0 :(得分:9)
答案 1 :(得分:6)
使用startActivityforResult
打开活动B ..然后在活动A中覆盖onActivityResult(int, int, Intent)
..
示例:
public class MyActivity extends Activity {
...
static final int PICK_CONTACT_REQUEST = 0;
protected boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
// When the user center presses, let them pick a contact.
startActivityForResult(
new Intent(Intent.ACTION_PICK,
new Uri("content://contacts")),
PICK_CONTACT_REQUEST);
return true;
}
return false;
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
// A contact was picked. Here we will just display it
// to the user.
startActivity(new Intent(Intent.ACTION_VIEW, data));
}
}
}
}
检查http://developer.android.com/reference/android/app/Activity.html
答案 2 :(得分:0)
使用startActivityforResult启动Activity B.在Activity A中实现覆盖onActivityResult(int,int,Intent)方法,在ActivityB中实现setResult。
示例:
public class ActivityA extends Activity {
static final int REQUEST_CODE = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.xyz);
DateBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivityForResult(new Intent(ActivityA.this,ActivityB.class), REQUEST_CODE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// A contact was picked. Here we will just display it
// to the user.
startActivity(new Intent(Intent.ACTION_VIEW, data));
}
}
}
}
public class ActivityB extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.xyz);
BackBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("DATA", "your string");
setResult(RESULT_OK, intent);
finish();
}
});
}
}