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?