0

I've got a Container with multiple fragments.

I tried implementing the following in the first fragment and then sending back data using a Bundle from the second fragment.

 @Override
public void onFragmentResult(int requestCode, int resultCode, Bundle data) {
    super.onFragmentResult(requestCode, resultCode, data);
    if (requestCode == REQ_MODIFY_FRAGMENT && resultCode == RESULT_OK && data != null) {
        mTitle = data.getString(KEY_RESULT_TITLE);
        mToolbar.setTitle(mTitle);
        getArguments().putString(ARG_TITLE, mTitle);
        Toast.makeText(_mActivity, R.string.modify_title, Toast.LENGTH_SHORT).show();
    }
}

But when I go to the third fragment (they are all in a sequence) and I go out of the app e.g to add an event to the calendar or to load Google maps from co-ordinates, the app fails with a continuous Parcel error. I've gone through all of my objects and parcels and none of them are incorrect.

As soon as I remove the onFragmentResult from Fragment one, the app stops crashing. I've given it a good few hours, but can't get my head around it. Any help folks?

Here's some output from my Logcat

    at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1579)
    at android.os.Bundle.writeToParcel(Bundle.java:1233)
    at android.os.Parcel.writeBundle(Parcel.java:931)
    read: unexpected EOF!

The above output is repeated a few tens of times and ends with EOF!

ibyte
  • 463
  • 4
  • 17

1 Answers1

0

I've found the answer finally. When in the second fragment, I was setting the setFragmentResult onCreateView which was causing a fail every time I left the app and came back to it. I resolved this by moving the code into the back button of the Fragment as below:

    @Override
public boolean onBackPressedSupport() {

    Bundle bundle = new Bundle();
    bundle.putBoolean("data_refreshed", true);
    setFragmentResult(RESULT_OK, bundle);

    Toast.makeText(_mActivity, "onBackPressedSupport-->return false, " + getString(R.string.upper_process), Toast.LENGTH_SHORT).show();
    return false;
}

Where onBackPressedSupport is a custom function I've created on my own toolbar. Thanks Blink Kai for just keeping me inspired to keep going.

Update: I've know implemented this in the onDestroyView of the fragment. Lot easier instead of writing my own custom function:

    @Override
public void onDestroyView() {
    super.onDestroyView();
    Bundle bundle = new Bundle();
    bundle.putBoolean("data_refreshed", true);
    setFragmentResult(RESULT_OK, bundle);
}
ibyte
  • 463
  • 4
  • 17