Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onRestoreInstanceState crash when Fragments in ViewPager #651

Closed
pmellaaho opened this issue Aug 16, 2018 · 0 comments
Closed

onRestoreInstanceState crash when Fragments in ViewPager #651

pmellaaho opened this issue Aug 16, 2018 · 0 comments

Comments

@pmellaaho
Copy link

pmellaaho commented Aug 16, 2018

Hello,

I can still get the App crash in the setup I explained in #649

I create the adapter (with no items) in onActivityCreated and my code for saving/restoring the state looks like this:

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Timber.e("onSaveInstanceState FLEX_ADAPTER")
        if (::adapter.isInitialized && adapter.mainItemCount > 0) {
            Timber.e("SAVE FLEX_ADAPTER STATE")
            adapter.onSaveInstanceState(outState)
        }
    }
    override fun onViewStateRestored(savedInstanceState: Bundle?) {
        super.onViewStateRestored(savedInstanceState)

        savedInstanceState?.let {
            Timber.e("RESTORE FLEX_ADAPTER STATE")
            adapter.onRestoreInstanceState(savedInstanceState)
        }
    }

To reproduce the error I follow these steps:

  • navigate to fragment A, swipe to B and then to C
  • Change orientation to landscape and back to portrait
  • Swipe to fragment B --> crash
java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.Collection.iterator()' on a null object reference
        at java.util.AbstractCollection.addAll(AbstractCollection.java:74)
        at java.util.TreeSet.addAll(TreeSet.java:132)
        at java.util.Collections$SynchronizedCollection.addAll(Collections.java:390)
        at eu.davidea.flexibleadapter.SelectableAdapter.onRestoreInstanceState(SelectableAdapter.java:577)
        at eu.davidea.flexibleadapter.FlexibleAdapter.onRestoreInstanceState(FlexibleAdapter.java:5106)

And the trace looks like this:

> onSaveInstanceState FLEX_ADAPTER
    SAVE FLEX_ADAPTER STATE
> onSaveInstanceState FLEX_ADAPTER
> RESTORE FLEX_ADAPTER STATE

So, saving and restoring do go out of synch!

It's possible to work around this problem by saving e.g. boolean flag to bundle in onSaveInstanceState and read it in onViewStateRestored before calling adapter.onRestoreInstanceState but could you consider checking if savedInstanceState.getIntegerArrayList(TAG) is null like mentioned in #611

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants