Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android getting RESULT_CANCELED when I specifically add RESULT_OK

Tags:

This is my problem, I have the main view which only shows one button, pressing this button another view is shown. This view has only another button, when this button is push this current view finishs and the control backs to the previous view.

To show the second view I use startActivityForResult, I put the code here.

private void startNewview() {       
    Intent it = new Intent(getApplicationContext(), newView.class);
    startActivityForResult(it,VIEW_ID);

}

The view called only has a button event, here is the code

Button b = (Button) findViewById(R.id.close);
    b.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            setResult(RESULT_OK);               
            finish();

        }
    });

And finally, the method onActivityResult in the main view, here is the code

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == VIEW_ID && resultCode == RESULT_OK) {
        tv = (TextView) findViewById(R.id.tv);
        tv.setText("The result ok is  here :)");
    }

}

The problem is resultCode always is 0 = RESULT_CANCELED and I do not know how to solve it, can anyone help me?

Thank you very much!

like image 379
F.bernal Avatar asked Feb 04 '11 12:02

F.bernal


1 Answers

here,

@Override
public void onBackPressed() {
    setResult(Activity.RESULT_OK);
    finish();
}

does work to return(RESULT_OK) by pressing the BACK button. Do NOT call

super.onBackPressed().

like image 197
chksr Avatar answered Sep 29 '22 11:09

chksr