Robustness
What brings a mobile app down (exception, blocked main thread, process death, rotation, memory, unexpected response), reading a crash trace, testing and tracking crashes.
- 28 cards
- 5 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Scenarios
- Understand
A few cards from the deck
Robustness: what is it?
Show the answer
An app's ability to keep working, or to fail cleanly, under unforeseen conditions: network down, missing data, low memory.
On mobile, unforeseen conditions are the norm: the user rotates the screen, loses the network, gets a call. Code that only works on the happy path soon crashes.
In this trace, which line is the first to belong to the
com.example.shopapp's own code?
```text
FATAL EXCEPTION: main
Process: com.example.shop, PID: 4821
java.lang.IllegalStateException: Fragment ProfileFragment{8f3c2a1} not attached to a context.
at androidx.fragment.app.Fragment.requireContext(Fragment.java)
at com.example.shop.ProfileFragment.showError(ProfileFragment.kt:58)
at com.example.shop.ProfileFragment.onResult(ProfileFragment.kt:41)
at android.os.Handler.handleCallback(Handler.java)`at com.example.shop.ProfileFragment.showError(ProfileFragment.kt:58)java.lang.IllegalStateException: Fragment ProfileFragment{8f3c2a1} not attached …at androidx.fragment.app.Fragment.requireContext(Fragment.java)at com.example.shop.ProfileFragment.onResult(ProfileFragment.kt:41)
Show the answer
at com.example.shop.ProfileFragment.showError(ProfileFragment.kt:58)The first line gives the exception, not a place in the code.
requireContextis framework: it throws because the fragment is no longer attached. The first app line isProfileFragment.kt:58, inshowError, which called it too late;onResult, lower down, is only its caller.To keep an app from becoming unresponsive, every … or disk access happens off the … thread.
Show the answer
To keep an app from becoming unresponsive, every network or disk access happens off the main thread.
Android even throws
NetworkOnMainThreadExceptionfor a network call made on the main thread. Kotlin coroutines or an executor move the work elsewhere.Crash: what is it?
Show the answer
An abrupt close of the app while in use, caused by an exception nothing caught or by a fatal error in native code.
On Android, the system reports that the app has stopped and writes a trace in the log (Logcat): that trace is what you read to find the faulty line. A process killed in the background to free memory is not a crash.
On tapping "Import", the screen freezes for 10 seconds and then Android offers to close the app. The code reads an 80 MB file in the click handler. What is the fix?
- Catch the exception with a try/catch around the file read inside the click handler
- Read the file in a coroutine or worker thread
- Read the file twice in a row to make sure its content is complete and valid
- Ask the user for the storage permission before each read of the file
Show the answer
Read the file in a coroutine or worker thread
No exception is thrown: the main thread is simply busy. A try/catch does not change the duration. The long work must leave the main thread, which only shows progress and the result.