Skip to content

NPE on View.dispatchDetachedFromWindow #87

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

Open
muthuraj57 opened this issue Jan 17, 2019 · 1 comment
Open

NPE on View.dispatchDetachedFromWindow #87

muthuraj57 opened this issue Jan 17, 2019 · 1 comment

Comments

@muthuraj57
Copy link

I have a fragment which contains FrameLayout with RecyclerView. When I replace or do any transaction on the fragment while a header is stickied in the RecyclerView, getting this crash.

I found the root cause of the crash. I'm setting my adapter as null on onDetachedFromWindow method of RecyclerView to get onDetachedFromRecyclerView callback on my RecyclerView to cleanup some resources (More about this https://github.com/airbnb/epoxy/wiki/Avoiding-Memory-Leaks, I'm not using Epoxy btw).

This calls removeAndRecycleAllViews which in turn calls positioner.clearHeader() which calls detachHeader. Here the header view is removed from the FrameLayout and that's where the issue is. I saw that you already experienced similar issue and handled it through safeDetachHeader by posting the removing process to run on later. I tried the same in detachHeader and it works fine now.

private void detachHeader(int position) {
        if (currentHeader != null) {
            recyclerView.post(new Runnable() {
                @Override
                public void run() {
                    getRecyclerParent().removeView(currentHeader);
                }
            });
//            getRecyclerParent().removeView(currentHeader);
            callDetach(position);
            currentHeader = null;
            currentViewHolder = null;
        }
    }

Not sure if the fix is correct, hence opening an issue instead of PR.

@muthuraj57
Copy link
Author

Update: This change crashes the app often when header is detached and attached again.

sdelaysam added a commit to sdelaysam/StickyHeaders that referenced this issue Dec 7, 2020
sdelaysam added a commit to sdelaysam/StickyHeaders that referenced this issue Jun 25, 2021
Attempt to fix bgogetap#87. Call ViewHolder.onViewRecycled on sticky header d…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant