setResult不调用onActivityResult

时间:2011-10-21 08:31:26

标签: android

我有两个活动为结果启动相同的子活动。 但只有其中一个onActivityResult工作 以下是我的代码部分

这是我的第一个活动。正如您所看到的,它在startActivityForResult中调用了NewCommentActivity,这部分工作正常

public class OfferActivity extends ListActivity implements View.OnClickListener {

...

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.offer_menu_comment:
            Intent commentIntent = new Intent(this, NewCommentActivity.class);
            commentIntent.putExtra(Offer.OFFER, offer);
            startActivityForResult(commentIntent, COMMENT_REQUEST_CODE);
            return true;

       ...

        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case COMMENT_REQUEST_CODE:
            if (resultCode == RESULT_OK) {
                Integer commentCnt = offer.getCommentCnt() + 1;
                offer.setCommentCnt(commentCnt);
                commentButton.setText(getString(R.string.offer_comments, offer.getCommentCnt()));
            }
            break;
    }
}

...

}

这是我的第二个调用NewCommentActivity的活动,但这里没有调用onActivityResult

public class CommentsActivity extends ListActivity{

...

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        ...
        case COMMENT_ITEM_ID:
            Intent commentIntent = new Intent(this, NewCommentActivity.class);
            commentIntent.putExtra(Offer.OFFER, offer);
            startActivityForResult(commentIntent, COMMENT_REQUEST_CODE);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case COMMENT_REQUEST_CODE:
            if (resultCode == RESULT_OK) {
                Integer commentCnt = offer.getCommentCnt() + 1;
                offer.setCommentCnt(commentCnt);
                Comment comment = data.getParcelableExtra(Comment.COMMENT);
                commentImageListAdapter.addItemAtFront(comment);
            }
            break;
    }
}

...

}

这是NewCommentActivity的代码:

public class NewCommentActivity extends Activity implements View.OnClickListener {
private static final String TAG = "Gift/NewCommentActivity";
private Offer offer;
private EditText editText;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.new_comment);
    offer = getIntent().getParcelableExtra(Offer.OFFER);

    editText = (EditText) findViewById(R.id.new_comment_text);

    Button button = (Button) findViewById(R.id.new_comment_button);
    button.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    if (editText.getText().length() > 0) {
        new AddCommentAsyncTask().execute(offer, editText.getText().toString());
    }
}

private class AddCommentAsyncTask extends AsyncTask<Object, Void, Comment> {
    private String errorString;
    private ProgressDialog progressDialog;

    @Override
    protected Comment doInBackground(Object... objects) {
        Offer offer = (Offer) objects[0];
        String text = (String) objects[1];
        Comment comment = null;
        try {
            comment = ServiceContainer.getService().addComment(offer, text);
        } catch (OfferException e) {
            Log.e(TAG, "exception in comment", e);
            int id;
            if ((id = e.getErrorStringId()) != -1) {
                errorString = getString(id);
            } else {
                errorString = e.getLocalizedMessage();
            }
            cancel(false);
        }
        return comment;
    }

    @Override
    protected void onPreExecute() {
        progressDialog = Utils.newSpinnerDialog(NewCommentActivity.this, R.string.sending);
    }

    @Override
    protected void onCancelled() {
        progressDialog.dismiss();

        AlertDialog.Builder builder = Utils.newAlertDialog(NewCommentActivity.this, errorString, true);
        builder.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
                new AddCommentAsyncTask().execute(offer, editText.getText().toString());
            }
        })
                .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        setResult(RESULT_CANCELED);
                        finish();
                    }
                })
                .create()
                .show();
    }

    @Override
    protected void onPostExecute(Comment comment) {
        progressDialog.dismiss();

        Intent intent = new Intent();
        intent.putExtra(Comment.COMMENT, comment);
        setResult(RESULT_OK, intent);
        finish();
    }
}

最后AndroidManifest用于所有活动:

         

    <activity android:name=".activity.OfferActivity"
              android:configChanges="orientation"/>
    <activity android:name=".activity.CommentsActivity"
              android:configChanges="orientation"
              android:label="@string/comment_activity_title"/>
    <activity android:name=".activity.NewCommentActivity"
              android:windowSoftInputMode="adjustResize"
              android:configChanges="orientation"
              android:label="@string/new_comment_activity_title"/>
</application>

请帮帮我。 感谢。

0 个答案:

没有答案