这里是我的问题:
Create a MainActivity. Add a button which will start another activity SecondActivity.
Intent i = new Intent(getActivity(),SecondActivity.class); startActivityForResult(i,0);
Inside the SecondActivity, I capture the back button click event and also add a button to return to the first Activity.
When back button in action bar is clicked:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// back button
Intent resultIntent = new Intent();
// TODO Add extras or a data URI to this intent as appropriate.
setResult(Activity.RESULT_OK, resultIntent);
//finish();
return false;
}
return super.onOptionsItemSelected(item);
}
当活动内的按钮被点击时:
Button btn = (Button)this.findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent resultIntent = new Intent();
// TODO Add extras or a data URI to this intent as appropriate.
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
});
当我在SecondActivity内点击按钮时,MainActivity中的onActivityResult被调用,但如果我点击SecondActivity中的Actionbar中的返回按钮,则该方法从未被调用。有人能告诉我原因吗?谢谢。