0

I encountered a bug about transferring data from Fragment to Activity. Here, I want the activity side data to be returned. But when searching, the onActivityResult teacher is not called.

Surprisingly, with this code, I used Realm to run it, it worked, but when I used Room it didn't work.

I tried several ways but couldn't:

  • Clean Project -> Rebuild Project.
  • Check the version of Room.
  • Search on stackoverflow.

My code

NoteFragment.java

private void sendData(int position) {
    Intent intent = new Intent(getActivity(), EditActivity.class);
    Bundle bundle = getBundleSend(position);
    intent.putExtras(bundle);

    startActivityForResult(intent, Constants.KEY_REQUEST_CODE);
}

// I tried log but i't not called
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == Constants.KEY_REQUEST_CODE && resultCode == Constants.KEY_RESULT && data != null) {
        Note note = (Note) data.getSerializableExtra(Constants.KEY_INTENT_SEND_ACTIVITY);

        int position = Validate.getInstance(getActivity()).findIndexWithId(mNotes, note.getId());
        mNotes.set(position, note);
        instanceData.noteDao().updateNote(note);
    }
}

EditActivity.java

private void saveData() {
    Intent intent = new Intent(this, NoteFragment.class);
    // update data before send
    noteEdited.setContent(tedt_content_edit.getText().toString());
    noteEdited.setChecked(cb_content_edit.isChecked());
    noteEdited.setNameSublist(tv_sublist_name_edit.getText().toString());

    intent.putExtra(Constants.KEY_INTENT_SEND_ACTIVITY, noteEdited);
    setResult(Constants.KEY_RESULT, intent);

    finish();
}

--

UPDATE

I have solved the problem. I have added a line at AndroidManifest

android: launchMode = "singleTask"

Reason: I only know that when running, the program creates 2 activities, even more clearly, it does not know. Can someone help me explain this problem?


Zeroes
  • 142
  • 1
  • 10
  • What's the value of KEY_RESULT ? – ror Apr 02 '20 at 06:30
  • I left it in a Constants class. **KEY_REQUEST_CODE = 9999; KEY_RESULT = 1111;** – Zeroes Apr 02 '20 at 06:33
  • Ok why don't you try to use Activity.RESULT_OK instead? Let's see how it works. – ror Apr 02 '20 at 06:34
  • Oh, and please just use new Intent() (constructor without params) – ror Apr 02 '20 at 06:36
  • I tried use Activity.RESULT_OK but it not work. new Intent() for EditActivity or Fragment. Thanks bro – Zeroes Apr 02 '20 at 06:37
  • https://stackoverflow.com/a/6147919/2215364 @zeroes please check this link. are you overriding onActivityResult in Activity of this fragment? – Nil Apr 02 '20 at 06:38
  • I tried the Intent at EditActivity, but it didn't work. The current problem is that onActivityResult is not called. – Zeroes Apr 02 '20 at 06:40
  • No. You should not be doing this new Intent(this, NoteFragment.class). Instead you should be doing this new Intent() – ror Apr 02 '20 at 06:44
  • not at EditActivity. EditActivty sending results and you have a problem in receiving. Check the activity of that fragment. If you have not written super statement in activity then onActivityResult of fragment will not call. – Nil Apr 02 '20 at 06:45
  • Sorry I don't understand anything. Activity still receives data, currently the problem is probably somewhere on Activity so the new method onActivityForResult does not receive. – Zeroes Apr 02 '20 at 06:51
  • @Nil **Thank you very much**. In `onActivityForResult`, I wrote `super.onActivityResult (requestCode, resultCode, data) ;`. I have read your article over and over but it seems that it is not the answer to my answer. – Zeroes Apr 02 '20 at 06:55
  • Welcome :) Well done. A good developer always knows where to find solution. :) Happy coding – Nil Apr 02 '20 at 07:02

0 Answers0