In the Docs, it says of onRestart (emphasis mine):
Called after onStop() when the current activity is being re-displayed to the user (the user has navigated back to it). It will be followed by onStart() and then onResume().
Now in my app, as recommended, I (attempt to) connect to the 'Google API Client' in onStart and disconnect in onStop like so: (simplified)...
@Override
public void onStart(){
super.onStart();
mGoogleAPIClient.connect();
}
@Override
public void onStop(){
super.onStop();
mGoogleAPIClient.disconnect();
}
I accidentally launched my app and immediately pressed home key get get rid of it, it went into the background, and then a few seconds later, popped back into life and started to attempt to connect the googleAPIClient. This only happened if the home key (or the overview key for that matter) was pressed quickly - ie, before the connecting dialogue had a chance to appear (or put another way, before mGoogleApiClient.connect() was even called).
Upon further testing, it became obvious that if I removed mGoogleAPIClient.connect() from onStart, it stayed in the background as was expected/required. I also confirmed that when it happens, onRestart is being called, and indeed, it is being called before onStart is invoked. However, this doesn't make any sense to me.
If the problem is being caused by mGoogleAPIClient trying to connect (and when doing so, bringing the activity back to the foreground), and mGoogleAPIClient.connect() is being called in onStart which is called after onRestart, how can onRestart know to be called if the event has not happened at that point?
I'd also confirm that I've tried at least 2 other apps from the Play Store that also exhibit this behaviour.
It's a problem that could easily pop up as it's all too easy to launch something by mistake and then send it to the background straight away, so I have implemented a workaround using boolean flags and now the problem only happens if you launch, press home/overview and immediately re-launch and background the app again (all before the dialogue comes up). I don't expect anyone to do this, but I'm left wondering, is this a bug of am I doing something wrong here or misunderstanding something and how do I stop this unwanted behaviour?
The Play Games dialog you are talking about requires some time to be laid out (guess play services has to connect to their APIs), so chances are that:
onStart()
is calledonRestart()
and onStart()
are called in this order, just like the docs say. At this point the connect()
call is useless.Since this is internal behavior, there’s not much we can do in my opinion, other than disconnecting the client onStop()
. If you feel this is important, you might try to file a bug / feature request.