https://kotlinlang.org logo
#android
Title
# android
d

Doru N.

02/17/2020, 5:05 PM
Hi there! It seems like there is little to none information about the fact that android recreates the activity when an exception is thrown inside a ViewModel class (instead of just crashing the app). Upon doing so, some of my app resources are left in an inconsistent state. I would rather let it crash instead. Can anyone help me with some information in this regard?
c

Casey Brooks

02/17/2020, 5:20 PM
I don’t think that is Android ViewModel behavior, but you might see that if you have Crashlytics, Fabric, or other crash reporting tools installed
d

Doru N.

02/17/2020, 5:22 PM
I created a simple app to reproduce the behavior: throwing the exception on button click from Activity -> crashed the app, but doing so from the ViewModel, recreates the activity first, and the second time crashes the app
w

wasyl

02/17/2020, 6:45 PM
How would that even work? It’s not that all the calls in view model are magically wrapped in try..catch 🤔
z

zhuinden

02/17/2020, 11:20 PM
this only happens if you use
FLAG_ACTIVITY_NEW_TASK
iirc
d

Doru N.

02/18/2020, 8:29 AM
@zhuinden Indeed, there seems to be something related to starting a new activity when reproducing this issue, yet I do not still understand why.. as I am starting the new Activity using the simple
Copy code
startActivity(Intent(this, MainActivity::class.java))
I do not set a
FLAG_ACTIVITY_NEW_TASK
anywhere (niether Manifest, nor directly on Intent starting this new Activity)
Also, crashing inside the Activity, always displays the Crash popup and closes the app, but doing so inside the ViewModel, <sometimes> it just “eats” the crash, and closes current Activity, displaying the previous screen as if nothing bad happened.
I kept on testing, and I discovered that the crash does not have to be inside the ViewModel.. it can be reproduced in an Activity also, but first time only, crashing a second time will display the dialog. Is this a native behavior?! (my test project has
compileSdk
&
targetSdk
set to
28
)
w

wasyl

02/18/2020, 8:48 AM
, <sometimes> it just “eats” the crash, and closes current Activity, displaying the previous screen as if nothing bad happened.
not all crashes are shown as dialogs
In developer options there’s
always show crash dialog
option. Iirc at some point Android stopped showing each crash, since the user can sometimes even miss that the app has crashed (it just seems like recreating the activity)
d

dawidhyzy

02/18/2020, 11:42 AM
Guys, this is off-topic
👮‍♂️ 1
Please see the channel topic.
6 Views