Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANR in com.android.launcher after tapping back

Tags:

android

When I install my app on certain devices (seems to be 4.4.4 and later) I sometimes see the following ANR in com.android.launcher after the following steps:

  1. From Activity A, launch Activity B
  2. In Activity B, press system back key and in the onBackPressed() method of Activity B relaunch Activity A (code is below).
  3. System back key remains in pressed state for 5 seconds, the screen turns black, my app gets closed and Launcher comes up. Investigating the logs shows an ANR in the Launcher process.

ANR in com.android.launcher (com.android.launcher/com.android.launcher2.Launcher) Reason: Input dispatching timed out (Waiting because no window has focus but there is a focused application that may eventually add a window when it finishes starting up.)

Notice that the ANR is occurring in com.android.launcher (not in my app).

Btw, I am NOT doing an expensive operation in my onBackPressed/onPause/onStop methods.

The logs also show that onBackPressed() is not being invoked when the ANR occurs - very odd behavior as you can see here:

## User starts Activity A from Android Launcher
D/Activity A: onResume()
D/Activity A: onClick(): start Activity B
D/Activity A: onPause()
D/Activity B: onResume()

## User taps system back key (and onBackPressed() launches Activity A)
D/Activity B: onBackPressed(): start Activity A
D/Activity B: onPause()
D/Activity A: onResume()
D/Activity A: onClick(): start Activity B
D/Activity A: onPause()
D/Activity B: onResume()

## User taps system back key (but onBackPressed() is not invoked. Instead an ANR occurs)
I/InputDispatcher(  557): ANR in com.android.launcher
D/Activity B: onPause()

Here is my onBackPressed() method:

public void onBackPressed() {
    Intent intentStartRoot = new Intent().setComponent(new ComponentName(this, ActivityHome.class));
    intentStartRoot.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(intentStartRoot);
}
like image 695
Jo Jo Avatar asked Jul 14 '15 22:07

Jo Jo


1 Answers

This is probably a bug in the Android Framework. I tested on about 12 devices today and noticed that this bug occurs reliably on Nexus-type devices running these versions of Android:

  • 4.4.4
  • 5.0.1
  • 5.0.2

Interestingly, Samsung devices running the same versions of Android do not exhibit the problem. Android 5.1 and later seems to be better in that I see no more ANRs in com.android.launcher; however I am now seeing ANRs in com.google.android.googlequicksearchbox.

I worked around this problem after noticing that my Activity was overriding onBackPressed() without invoking super.onBackPressed(). After I added super.onBackPressed(), the ANRs went away. But this is not a full workaround because calling super.onBackPressed() finished my Activity, which I do not want.

I hope this helps someone.

See: Using FLAG_ACTIVITY_REORDER_TO_FRONT to switch among persistently running UI activities leads to "no window focus" error

See: Android-L issue: onBackpressed when using FLAG_ACTIVITY_REORDER_TO_FRONT to launch previous activity & freezes app for sometime

Also, I found a bug entry on Google's site that seems related: https://code.google.com/p/android/issues/detail?id=91534

like image 103
Jo Jo Avatar answered Nov 09 '22 11:11

Jo Jo