I am currently working on a Android-counter-app written in Kotlin.
On the apps startup, I'm fetching all the information needed on the start-up screen. From there, everything is sequentially being fetched, as more fragments are being loaded.
When the app is being started for the very first time, there obviously isn't any data to fetch.
My solution therefore is, to just fetch everything needed and just save some null values. After that, each fragment has some validation methods to ensure the data retrieved is not null. If the data on the first screen however, is null, I'm redirecting the user to a screen to fill in this missing data.
And as already written in the title: The navcontroller in my start-up fragment is not working properly.
To switch in between fragments, I usually just call findNavController().navigate(R.id.action_a_to_b)
. This however seems to not be working with my current solution:
//This method fetches a counter from my datahandling service.
private fun fetchMainCounter(c: Counter?) {
if (c == null)
findNavController().navigate(R.id.action_start_to_edit)
fetchedMainCounter = c
}
The problem seems to be, that even though c is null
and the "pointer" goes through the navcontroller, the last bit of the method is still being executed and therefore the code continues running perfectly fine until the point, where the missing data is needed.
I solved my problem by simply returning another object of the same data class, filled with mock data.
private fun getCounter(c: Counter?): Counter {
return try {
c!!
} catch (e: Exception) {
e.printStackTrace()
Counter(null, null, null, -1L)
}
}
After that I'm checking said counter for the mock data in a separate method.
private fun checkForNullValues(c: Counter): Boolean {
return c.pOne.equals(null)
&& c.pTwo.equals(null)
&& c.dateDiff == -1L
}
Finally, I'm putting the result of the getCounter() method through an if else block.
if (checkForNullValues(c))
findNavController().navigate(R.id.action_start_to_edit)
else updateMainCounter(c)
Feel free to improve this answer! I know this is not the best solution out there, but I just wanted to finish this project :)