My app fails with Parcel error when Implementing startForResult

117
August 13, 2019, at 06:00 AM

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!

Answer 1

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);
}
Rent Charter Buses Company
READ ALSO
Is there a way for webView to permanently save and store caches from a service worker?

Is there a way for webView to permanently save and store caches from a service worker?

I've got a webapp for android, which is using webview to display my webpageMy webpage has a service worker on index

139
How to rotate one circle anti clockwise canvas android

How to rotate one circle anti clockwise canvas android

I can't solve problem and I need helpI create planets around sun to rotate and I need one planet t rotate anti clockwise and my problem begins

120
How to detect key-guard state in android

How to detect key-guard state in android

I have an application that requires to detect the previous state of the key-guard before calling disableKeyguard() or reenableKeyguard(), set by the user! is there any way of accomplishing this task?

157
Xiaomi phones not showing runtime permission request - Android 9.0

Xiaomi phones not showing runtime permission request - Android 9.0

My Android app requires to write some data file to phone's storageAnd I request the permission during runtime as specified for Android >8

153