Search code examples
androidzxingandroid-recyclerviewgridlayoutmanager

GridLayoutManager spanSizeLookup bug


I am having an issue with GridLayoutManger setSpanSizeLookup, when an orientation of activity changes, I am changing the span count while checking if the specific position is some sort of type, this works perfectly on orientation change, my only problem is, I'm using zxing library to do barcode scanning, whenever a button is clicked I'm opening the zxing default intent and retrieve date from it, however when zxing is opening it goes to landscape and my current activity orientation is portrait this gives me the IllegalArgumentException that the layout manage throws when only opening the Xzing intent, the crash log is as this
java.lang.IllegalArgumentException: Item at position 0 requires 2 spans but GridLayoutManager has only 1 spans.
this problem doesn't occur if i rotate the phone, only happens when I launch the Xzing intent, i couldn't really figure out how how I solve this issue as its bugging me. here is my spanSizeLookup -

manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
     @Override
     public int getSpanSize(int position) {
        return adapter.getHolders().get(position).getLabelHolder() != null ? getResources().getInteger(R.integer.span) : 1;
     }
});

the span is base on screen size so it can be 1-2, 2-3 and 3-4. i repeat this doesn't give me any error on orientation change, only the error occur when I open the zxing
P.S if i open zxing intent while my activity is on landspace the crash wont occur.

Edit

it seems like only launching the zxing default intent causing this issue, I'm having an activity where it goes landscape after it launches and the exception didn't occur, as a workaround i did, was that i created an activity that handles the barcode scanning with a delay of 1 second to launch the intent as if i didn't do that, it will throw the same exception.

Edit 2

I just found that, even without setSpanSpizeLookup the crash still occurs. by just calling manager.setSpanCount(getResources().getInteger(R.integer.span))


Solution

  • Too late to answer this question, however in a latest project that i worked on, i faced the same issue & after some really debugging, i found out what was the cause to the issue but no clue why would it cause it.

    zxing Library intent it actually calls CLEAR_TOP before calling the startActivityForResult & that was the problem, after modifying the source code of zxing Intent to remove that particular line, the crash is gone.