从Activity返回结果

时间:2014-08-12 23:47:41

标签: java android android-intent android-activity navigation-drawer

我在Android 4.0.x上遇到问题。当向上导航 onActivityResult 时,resultCode始终为0且Intent为null。

导航代码:

    protected void navigateUp(){
       Intent upIntent = NavUtils.getParentActivityIntent(this);
       upIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

       if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
           TaskStackBuilder.create(this)
           .addNextIntentWithParentStack(upIntent)
           .startActivities();
       } else {
           NavUtils.navigateUpTo(this, upIntent);
       }
    }

设置结果代码:

    public void onNavigationDrawerItemSelected(int position) {
       Intent resultIntent = new Intent();
       resultIntent.putExtra(SELECTED_POSITION, position);
       setResult(RESULT_OK, resultIntent);
       navigateUp();
    }

有什么问题?它适用于4.4.x,不适用于4.0.x.为什么? THX!

1 个答案:

答案 0 :(得分:0)

您忘记了方法onActivityResult,因此您的代码必须如下:

Intent intent = new Intent(this, YourActivity.class);
startActivityForResult(intent, 1);
.
.
.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK){
        //Do whatever you want 
  }
}