Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pressing "Close App" in Android ANR dialog, closes and immediately relaunches app

I am facing a weird (in my opinion) issue. My app rarely produces ANRs. I am trying to reduce them wherever possible and i have been succesful so far. However, when the ANR dialog does appear, if i (or any user) decide to close the app by pressing "close app" rather than wait for it to get responsive, then the app closes but immediately gets relaunched displaying the same activity as it did when the ANR dialog got displayed.

This is troublesome because when a user normallly starts my app there is a certain sequence of activities he/she passes through, each responsible to perform some tasks, create some objects etc. When the app gets automatically relaunched however and the user finds himself in the same activity he was when the ANR dialog got displayed, this sequence of activities has not taken place, so certain things required are missing and thus my app crashes. It might be that an object is null, an int has wrong value than it should, some string may be empty etc. The exact problem is not important here. The important thing is that the app has not gone through the sequence of activities that it normally does when opened by a user , setting up all those objects,variables etc that it will later need before reaching this activity.

On the other hand, if my app crashes at any point, i have an UncaughtExceptionHandler that closes my app in a "clean way". The equivalent of a user pressing the "Overview" button and then closing the app either by pressing "x" or by swiping it left/right. So if the user wants to use the app again, he/she launches it again, the app goes through the sequence of activities it should and everything works as intended. However when an ANR dialog appears, i cannot control what pressing "Close App" actually does...

Or can i?

Is this the correct behaviour of the ANR dialog or am i missing something here? Has anyone else ever experienced this? Is there any way i can make my app close and stay closed after the user has selected the "Close app" option in the dialog? (I am guessing not).

Thank you in advance and please, if i am missing something obvious here, try to be gentle! :)

like image 524
Sotiris S. Magionas Avatar asked Oct 17 '22 16:10

Sotiris S. Magionas


2 Answers

It depends on "foreground" or "persistent" aspects which may be present in your application. Have a look at AppErrors.java

handleAppCrashLocked() is called before killing your processes - you can see the conditions defined here in which cases the activity/service will be restarted. Check if this applies to you.

Secondly if you are not working on AOSP you cannot modify the restart intent with which your application will be restarted. So your only option would be to detect that this is crash recovery and launch the appropriate screen. There will be a difference in intent which you get when your application is launched via crash recovery vs regular launch.

If you are working on AOSP you can follow the trail from AppErrors.java

like image 179
RocketRandom Avatar answered Nov 15 '22 06:11

RocketRandom


Have a look at this

Basically what that guy was trying to accomplish is making a log of the uncaught errors as you said in your post that you are unable to handle and making use of those logs for rectifying the inconsistencies in your app.

Closing the app generally does close the app and should not reopen the app, but that totally depends on the Dialogs implementation in your Mobile's OS, some OEM makes changes to the existing functionality and sometimes they do mess up.

You can also check for intent filters in your Manifest to check if those are interfering in some way or not.

like image 30
Kashif K. Avatar answered Nov 15 '22 07:11

Kashif K.