5

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! :)

  • I won't be gentle lol Take the time to remove all ANR's and your app will be much better for it. The other thing you can do is to put in some Log's and check logcat when it start to see how its starting after an ANR. If its not what you want then simply fix it based on what you're seeing - if something is null when it shouldn't be then do a check and if null then re-create so its not. Its not difficult, just requires time and thought and maybe some stress lol. – CmosBattery Jun 27 '18 at 11:23
  • @CmosBattery I won't disagree to anything you said. it might prove the only viable solution to do exactly that. However i would like an answer to whether closing and immediately relaunching the app is what pressing "Close App" in the ANR dialog is supposed to do. – Sotiris S. Magionas Jun 27 '18 at 11:27

3 Answers3

2

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

RocketRandom
  • 1,102
  • 7
  • 20
  • Thanks for your answer. I have not yet found the time to explore what you are suggesting but it looks promising. As the bounty is about to expire, i will reward your answer with it but i will accept your answer if it does indeed lead to a solution to my problem which i still regard as open. – Sotiris S. Magionas Jul 06 '18 at 13:10
1

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Kashif K.
  • 88
  • 1
  • 9
0

I fond a link here (https://mobikul.com/auto-restart-application-crashforce-close-android/). This is saying how you can auto restart a forcefully closed application. They provided a way to handle these errors that are not caught in try catch block. Practically I have not implemented this, because I was unable to generate ANR dialog. :-(

Hope it helps!

suprita
  • 211
  • 1
  • 9