I have an app which runs on a watch and where there is an incoming call it displays a translucent overlay activity on top of the OS's incoming call screen.
95% of the time this works great, but for the other 5% of the time my activity isn't visible on an incoming call. The difference is that when it doesn't work my activity's onPause() and onStop() get called right after its onCreate() i.e.
Log when it displays successfully:
03-27 10:04:41.958 onCreate()
03-27 10:04:41.981 onStart()
03-27 10:04:41.981 onResume()
Log when it doesn't display:
03-27 09:54:53.346 onCreate()
03-27 09:54:53.367 onStart()
03-27 09:54:53.367 onResume()
03-27 09:54:53.373 onPause()
03-27 09:54:53.437 onStop()
See how it jumps directly from onResume() to onPause(). My speculation for it doing this is that maybe its a timing issue and that in these 5% of failures my activity is being launched a fraction of a second before the incoming call screen's activity and that is causing it to move to the onPause() state. Though there's only microseconds between onResume() and onPause()
Question 1) Could there be any other reason why its going to onPause() 5% of the time?
I tried adding this code to rectify when this happens:
protected void onStop()
{
Log.i(TAG, " ACTIVITY onStop()");
super.onStop();
if (/*activity isn't being stopped because I am dismissing it*/)
{
moveToFront();
}
}
private void moveToFront()
{
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.AppTask> tasks = activityManager.getAppTasks();
Integer count = tasks.size();
// There should only ever be one task as a launchMode of singleInstance is used
ActivityManager.AppTask task = tasks.get(0);
task.moveToFront();
}
However this does not work as my activity isn't visible after moveToFront() is called.
Question 2) Does anybody have any suggestions for solutions for my problem? How I can always get my activity to display on top of the incoming call screen at all times?