google / hover

A floating menu library for Android.
Apache License 2.0
2.64k stars 284 forks source link

removing selected section causes invisible FAB (still draggable) #91

Open tsengstudios opened 5 years ago

tsengstudios commented 5 years ago

If mycustomHoverMenu removes a selected Section from mSections(the ArrayList<Section>) while it is expanded, then calling notifyMenuChanged() causes a crash. To demonstrate, change 1 to 0 on line 123 of MutatingSectionsHoverMenuService.java of the hoverdemo-helloworld app. And manually expand the FAB after starting the "Launch Changing Sections".

removeTab(0);

That change just targets the currently selected section/tab for removal instead of a non-selected section. If I collapse the hoverView first, I can avoid the crash, but then I've seen the FAB ends up invisible (still referring to the removed section if it is at the end of the list), or sometimes crash after manually clicking the FAB, but I wonder if there's a better/safer procedure to removing sections.

I realize this is unsupported. But just in case there's a community using this cool library, I thought I'd post this question. And hopefully, my workaround shortly.

The crash details:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: org.codecanon.hover.hoverdemo.helloworld, PID: 27913
    java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1
        at java.util.ArrayList.get(ArrayList.java:439)
        at io.mattcarroll.hover.hoverdemo.helloworld.MutatingSectionsHoverMenuService$MutatingHoverMenu.getSection(MutatingSectionsHoverMenuService.java:179)
        at io.mattcarroll.hover.HoverViewStateExpanded.removeSection(HoverViewStateExpanded.java:492)
        at io.mattcarroll.hover.HoverViewStateExpanded.removeSections(HoverViewStateExpanded.java:466)
        at io.mattcarroll.hover.HoverViewStateExpanded.access$600(HoverViewStateExpanded.java:39)
        at io.mattcarroll.hover.HoverViewStateExpanded$6.onRemoved(HoverViewStateExpanded.java:327)
        at android.support.v7.util.BatchingListUpdateCallback.dispatchLastEvent(BatchingListUpdateCallback.java:62)
        at android.support.v7.util.DiffUtil$DiffResult.dispatchUpdatesTo(DiffUtil.java:729)
        at io.mattcarroll.hover.HoverMenu.notifyMenuChanged(HoverMenu.java:76)
        at io.mattcarroll.hover.hoverdemo.helloworld.MutatingSectionsHoverMenuService$MutatingHoverMenu.removeTab(MutatingSectionsHoverMenuService.java:224)
        at io.mattcarroll.hover.hoverdemo.helloworld.MutatingSectionsHoverMenuService$MutatingHoverMenu.access$100(MutatingSectionsHoverMenuService.java:59)
        at io.mattcarroll.hover.hoverdemo.helloworld.MutatingSectionsHoverMenuService$MutatingHoverMenu$10.run(MutatingSectionsHoverMenuService.java:123)
        at io.mattcarroll.hover.hoverdemo.helloworld.MutatingSectionsHoverMenuService$MutatingHoverMenu$12.run(MutatingSectionsHoverMenuService.java:142)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6718)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
tsengstudios commented 5 years ago

My current workaround is to try preventing the crashes inside the Hover library. I added two if's into HoverViewStateExpanded.java.

    private void removeSection(int sectionIndex) {
        final FloatingTab chainedTab = mChainedTabs.remove(sectionIndex);
        TabChain tabChain = mTabChains.remove(sectionIndex);
        tabChain.unchain(new Runnable() {
            @Override
            public void run() {
                if (mHoverView != null)     // prevent use of null pointer
                    mHoverView.mScreen.destroyChainedTab(chainedTab);
            }
        });

        // If the removed section was the selected section then select a new section.
        HoverMenu.Section removedSection = mSections.get(chainedTab);
        if (removedSection.getId().equals(mHoverView.mSelectedSectionId)) {
            int newSelectionIndex = 0;
            if (sectionIndex - 1 < 0) {    // prevent index out of bounds 
                newSelectionIndex = 0;  // zero is a fine choice for the new selected section
            } else if (sectionIndex - 1 < mHoverView.mMenu.getSectionCount() - 1) {
                newSelectionIndex = sectionIndex - 1;
            } else {
                newSelectionIndex = mHoverView.mMenu.getSectionCount() - 1;
            }

            selectSection(mHoverView.mMenu.getSection(newSelectionIndex));
        }