armcha / armcha/Space-Navigation-View

Showing a badge throws IndexOutOfBoundsException if called from main Activity lifecycle methods

Open
#19 7 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
2k
Forks
376
PR merge metrics
No merged PRs in 30d

Description

Hey @armcha, really liking the library!
I set up my SpaceNavigationView in the onCreate of my Activity with the same setup shown in the readme and have added `bottomNav.onSaveInstanceState(outState);` in my onSaveInstanceState.
Everything looks and works great in the launched activity, SpaceOnClickListener events are returning the correct name and index for my items. But if I try to call

`spaceNavigationView.showBadgeAtIndex(0,5, ContextCompat.getColor(this, R.color.cyan));`

in any of the main lifecycle methods in my Activity (bottom of onCreate, onResume, onPostResume, etc.) the SpaceNavigationView throws an IndexOutOfBoundsException:
`Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.luseen.spacenavigation.SpaceNavigationView.showBadgeAtIndex(SpaceNavigationView.java:822)
at com.proto.tangible.tagalong.ExploreActivity$override.onResume(ExploreActivity.java:193)
at com.proto.tangible.tagalong.ExploreActivity$override.access$dispatch(ExploreActivity.java)
at com.proto.tangible.tagalong.ExploreActivity.onResume(ExploreActivity.java:0)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1326)
at android.app.Activity.performResume(Activity.java:6323)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3311)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3359) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2549) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4398) 
at android.app.ActivityThread.access$1000(ActivityThread.java:150) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:168) 
at android.app.ActivityThread.main(ActivityThread.java:5845) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) `

However, calling the same line some time after the Activity loads (in response to a manual click event or async task with Thread.sleep(400) works just fine for me!

Looking at the library source it seems like the SpaceNavigationView needs to have been laid out before its possible to show badges for your SpaceItems. In most situations the badge quantity to display is calculated almost instantly, so waiting for the draw is a little cumbersome

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in SpaceNavigationView.java at showBadgeAtIndex around line 822 and trace how the SpaceItems list is populated during the Activity lifecycle and layout. Reproduce the call from onCreate or onResume, compare it with the delayed call that succeeds, and consider the issue complete when showing a badge during those lifecycle methods no longer throws and displays the badge correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java
Domain
mobile-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.