As I was running a test for a ViewModel function, I encountered the following error: […] Main Thread @coroutine java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time. I think it may be caused by the viewModelScope from viewModelScope.launch {} being used by […]
Author: Catzie
Grant, revoke permissions via Android Studio Terminal (ADB Shell) // Android Dev Diary
To grant and revoke permissions via Terminal (ADB Shell), run your app on an emulator (I haven’t personally tried on a device but it should work there too). Open your Terminal, and then enter the following command: adb shell Once you’re in, you may use the following syntax for granting or revoking your Android device’s […]
Nasolaryngoscopy experience at Makati Med ENT Center
I went to Makati Medical Center the other day for a “flexible nasolaryngoscopy” procedure at the ENT Center. This was to examine my throat and see what’s going on there. A nurse sprayed topical anesthesia into my nostrils. Despite the numbness in my soft palate area, the insertion of the scope still felt quite uncomfortable, […]
Remove room database on app uninstall // Android Dev Diary
The problem: Installing on an Android device that has an old version of an app I’m developing triggers an error: Room Database Migration: java.lang.IllegalStateException: Migration didn’t properly handle Problem persists despite uninstalling the Android app and deleting all app data. The solution: Very simple. I set allowBackup to false in my AndroidManifest.xml file like so: […]
Get Navigation Component destinations in back stack // Android Kotlin Dev Diary
It’s simple to get the list of destinations that’s currently in your back stack. The only thing you need is a reference to the Navigation Component’s NavController. The NavController object has a backQueue property that you can inspect. backQueue is an array, and each item’s destination property is what you’re probably looking for! To show […]
A working shared ViewModel between parent and child Fragments //Android Kotlin Dev Diary
I initially had a problem with the shared ViewModel because the child fragments were creating a new instance from the one held by the parent fragment. The ViewModel instantiations below work for me: CreateLogFragment.kt (parent)
1 2 3 4 |
class CreateLogFragment : Fragment() { private val mainViewModel: MainViewModel by activityViewModels { MainViewModel.Factory } . . . |
CreateLogFoodFragment.kt (child)
1 2 3 4 5 |
class CreateLogFoodFragment : Fragment() { private val mainViewModel: MainViewModel by activityViewModels( null, factoryProducer = { MainViewModel.Factory }) . . . |
CreateLogNotesFragment.kt (another child)
1 2 3 4 5 |
class CreateLogFoodFragment : Fragment() { private val mainViewModel: MainViewModel by activityViewModels( null, factoryProducer = { MainViewModel.Factory }) . . . |
by activityViewModels turned things around!
Two NavHosts with their own NavControllers and NavGraphs (Navigation Component) //Android Kotlin Dev Diary
I am trying to make a single-activity app because I’d like to easily share data across screen using, ideally one, or as few as possible ViewModels. NavGraph 1 Host: Layout file of MainActivity Destination(s): CreateLogFragment NavGraph 2 Host: Layout file of CreateLogFragment Destination(s): CreateLogFoodFragment, CreateLogNotesFragment (CreateLogFragment has a BottomNavigationBar that switches between CreateLogFoodFragment and CreateLogNotesFragment) […]
My Fave Android Studio Keyboard Shortcuts
I have been working on small Android apps, and I just want to share some of my favorite keyboard shortcuts. Some of them were manually added by me. Keyboard shortcuts are one of my must-haves for productivity as a programmer, as they let me navigate through code faster, allowing me to focus on creating new […]
val vs. const //Kotlin Android Dev Diary
Let’s start off by introducing val. To define a variable in Kotlin, you may use var or val: var val has a mutable value (changeable) has an immutable value (read-only) So we use val for immutable values, so what is the const keyword and what’s its difference from val? const val has an immutable value […]
Dao Error: Not sure how to handle query method’s return type //Android Kotlin Dev Diary
As I brush up on my Room database knowlege I came upon the following error while building my app:
1 |
WordDao.java:21: error: Not sure how to handle query method's return type (java.lang.Object). DELETE query methods must either return void or int (the number of deleted rows). |
The solution: Set Kotlin version to 1.6.10 then Room to 2.4.2 (Or use more recent compatible versions) in my top-level build.gradle file. Reference: Android Room Kotlin throws Delete Query error