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 […]
Android app development
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!
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
Prevent Huawei from Killing Your App //Android Dev Diary
I have been playing around with Android app development, particularly on how to retain UI values after configuration changes and minimizing the app to launch a different one. To retain values after configuration changes, I use ViewModels. And to retain values after minimizing the app, the following is supposed to do the trick: But in […]
Android Kotlin: ‘getter for position int’ is deprecated. deprecated in java
Today I encountered a deprecation warning as I’m coding the app: ‘getter for position int’ is deprecated. deprecated in java And I was like, whoa, this wasn’t deprecated a couple of months back when I last touched Android code… this is how long I’ve missed out! 😂😂😂 I haven’t touched any app code for a […]
Android Kotlin: “overrides nothing” error appears on instantiateItem() function of PagerAdapter
I just began learning Kotlin for work and one of the first errors I encountered is “overrides nothing” pointing at the overrides of the function instantiateItem() for my PagerAdapter The function/method signature was like this: override fun instantiateItem(parent: ViewGroup?, position: Int): Any { To fix the error, I simply removed the question mark from the […]
Android: Facebook SDK “Invalid Key Hash” on Ubuntu
I kept getting a different Facebook key hash from the one being actually generated by my debug app when it runs. Turns out I’ve been entering the wrong keystore password. To get your key hash for Facebook SDK on Ubuntu, run the following command:
1 |
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64 |
In case you don’t have the keytool program yet, you’ll […]
Android: SharedPreferences not accessible from SyncAdapter
When I was trying to access a SharedPreferences value from my Android app’s SyncAdapter, I kept on receiving an old value that was saved to the key. Quick solution to inaccessible SharedPreferences from SyncAdapter A quick solution (which may be a short-term one) I tried was to remove android:process=”:sync” from the following code:
1 2 3 4 |
<service android:name=".service.SyncService" android:exported="true" android:process=":sync"> |
But […]
Android app dev: Intent extras set in onBackPressed() misssing when back to onActivityResult()
It’s funny how we spent about an hour trying to figure out why the Intent extras that we’re setting are not being sent back to onActivityResult(). If we only knew that the solution was as simple as moving one line of code. The call to super.onBackPressed() should be placed after setting Intent extras and after […]
Android app dev: Missing import org.apache.commons.lang3.text.WordUtils;
I use a few instances of WordUtils.capitalizeFully() on an Android app that I’ve been working on. And there are times when the import org.apache.commons.lang3.text.WordUtils; breaks, sometimes after merging with some of my git branches. To fix this, what I do is open app/build.gradle and add the following line under dependencies:
1 |
compile 'org.apache.commons:commons-lang3:3.4' |
Hope this saves somebody […]