This throws in some menus, to reproduce the following stack trace just open Search and toggle themes:
2020-03-09 17:58:46.516 10999-10999/com.materialstudies.owl E/rialstudies.ow: Unknown bits set in runtime_flags: 0x8000
2020-03-09 18:00:50.873 10999-10999/com.materialstudies.owl E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.materialstudies.owl, PID: 10999
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.materialstudies.owl/com.materialstudies.owl.ui.MainActivity}: java.lang.IllegalArgumentException: width and height must be > 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5279)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5187)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:1113)
at android.graphics.Bitmap.createBitmap(Bitmap.java:1080)
at android.graphics.Bitmap.createBitmap(Bitmap.java:1030)
at android.graphics.Bitmap.createBitmap(Bitmap.java:991)
at androidx.core.view.ViewKt.drawToBitmap(View.kt:181)
at androidx.core.view.ViewKt.drawToBitmap$default(View.kt:177)
at com.materialstudies.owl.util.ViewExtensionsKt.show(ViewExtensions.kt:194)
at com.materialstudies.owl.ui.MainActivity$onCreate$1$1.onDestinationChanged(MainActivity.kt:44)
at androidx.navigation.NavController.addOnDestinationChangedListener(NavController.java:204)
at com.materialstudies.owl.ui.MainActivity.onCreate(MainActivity.kt:42)
at android.app.Activity.performCreate(Activity.java:7825)
at android.app.Activity.performCreate(Activity.java:7814)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5279)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5187)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
It seems it restarts the MainActivity and forward the navigation but it fails in this extension:
fun View.drawToBitmap(config: Bitmap.Config = Bitmap.Config.ARGB_8888): Bitmap {
if (!ViewCompat.isLaidOut(this)) {
throw IllegalStateException("View needs to be laid out before calling drawToBitmap()")
}
return Bitmap.createBitmap(width, height, config).applyCanvas {
translate(-scrollX.toFloat(), -scrollY.toFloat())
draw(this)
}
}
Because width and height will be 0.
One way to fix this is adding lifecycleScope.launchWhenResumed
in MainActivity:
lifecycleScope.launchWhenResumed {
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.myCourses, R.id.featured, R.id.search -> bottomNav.show()
else -> bottomNav.hide()
}
}
}
Is this fix optimal? I can make a PR if needed.