0

In my main activity I call the second activity via button click in my toolbar like this

  if(id == R.id.addNav) {
        startActivityForResult(new Intent(this, TextFormActivity.class), 0);
        return true;
    }

Then in my second activity first I have constant for the tag

public static final String EXTRA_TEXT_ADD =
            "text_add";

Then when the add button was clicked I'm setting up the result

mButtonAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Text text = new Text();
            text.setTitle(mEditTextTitle.getText().toString());
            text.setRecipient(mEditTextRecipient.getText().toString());
            text.setSent(false);
            text.setMessage(mEditTextMessage.getText().toString());
            mTextList.add(text);
            Intent i = new Intent();
            i.putExtra(TextFormActivity.EXTRA_TEXT_ADD,true);
            setResult(RESULT_OK, i);

        }
    });

This is how I handle things in onActivityResult

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d("TextListActivity", "Result Entered");
        if(data == null){
            return;
        }
        boolean result = data.getBooleanExtra(TextFormActivity.EXTRA_TEXT_ADD,false);
        Toast.makeText(this,"Result Code: "+ result,Toast.LENGTH_SHORT).show();
    }

I still don't know why my toast is not showing up when I'm returning to the main activity

EDIT Well I already found the problem and that is I'm using NavUtils.navigateUpFromSameTask(TextFormActivity.this); to go back to the main activity and it does not call the method onActivityResult.

devadnqpnd
  • 154
  • 1
  • 3
  • 16
  • Is your debug message showing up in your logcat? If `data` were `null`, you'd not see your `Toast`. Are you even sure the result is being set properly in your other activity? – Ryan J Jul 29 '15 at 01:56
  • No it's not showing in the logcat. This is how i set it Intent i = new Intent(); i.putExtra(EXTRA_TEXT_ADD,true); setResult(RESULT_OK,i); – devadnqpnd Jul 29 '15 at 01:57
  • I set this inside a setOnclickListener – devadnqpnd Jul 29 '15 at 02:00

1 Answers1

1

You need to finish the activity you started for result.

So in you button click listener after setting the result , just finish the activity.

mButtonAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            .....
            ........
            Intent i = new Intent();
            i.putExtra(TextFormActivity.EXTRA_TEXT_ADD,true);
            setResult(RESULT_OK, i);
            finish();// finish the activity
        }
    });

Try this, should work.

Nicks
  • 16,030
  • 8
  • 58
  • 65