1

Excerpt from Google API Client documentation,

public GoogleApiClient.Builder enableAutoManage (FragmentActivity
fragmentActivity, GoogleApiClient.OnConnectionFailedListener
unresolvedConnectionFailedListener)

Enables automatic lifecycle management in a support library FragmentActivity that connects the client in onStart() and disconnects it in onStop().

It handles user recoverable errors appropriately and calls onConnectionFailed(ConnectionResult) on the unresolvedConnectionFailedListener if the ConnectionResult has no resolution. This eliminates most of the boiler plate associated with using GoogleApiClient.

While it may sound a bit naive, but my question is if this is an important step that we start the connection of a GoogleAPIClient in onStart method? Is there any harm if I make a connection in onCreate instead? I have multiple scenarios where I see the need to connect to the googleApiClient in onCreate due to the legacy code. I have also tried making a connection in onCreate and that works(I dont see any crashes). Is that a bad idea? So why is it necessary or important to start a connection in onStart and stop the connection in onStop?

Ahmed
  • 2,966
  • 7
  • 42
  • 69

2 Answers2

0

You may refer with this thread: What does onStart() really do? - Android

onStart() is called when activity resumes from stopped state. For example, if you have activity A and starts activity B from it, then activity A will be paused (onPause()) and then stopped (onStop()) and moved to back stack. After this, if you press Back into your activity B, B will be paused(onPause()), stopped (onStop()) and destroyed(onDestroy()), and activity A will be restored from back stack, started (onStart()) and resumed(onResume()). As you can see, system will not call onCreate() for A again.

How to use onStart()? For example, you should unregister listeners for GPS, sensors, etc in onStop() and register again in onStart(). If you register it in onCreate() and unregister in onDestroy(), then GPS service will work always and it will drain battery.

It was also stated that the main role of onStart() is when the activity is called after being paused. If onCreate() is called then all the initializations should be done again, by using onStart() we can initialize those things which had been uninitialized in onstop().

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59
0

Moving code from onStart to onCreate will cause problems. Take for example, an application that uses location services to display location on a map. This is a well-behaved application that stops using location services onStop, when the user is no longer using the application.

  1. User starts application (onCreate, onStart)
  2. User goes away (onStop)
  3. User comes back to application before it is destroyed (onStart, but not onCreate)

If you connect only upon onCreate, this means the application is not connected after step #3. The user will be using the application without the correct functionality because onCreate may not be called upon returning to an application.

mattm
  • 5,851
  • 11
  • 47
  • 77
  • Thanks for the reply @mattm. But if you lets say handle the connection by yourself, it doesnt cause any problems? This is only a recommendation correct not a must do right? Running in Exceptions, etc? – Ahmed Nov 07 '17 at 17:08
  • @Ahmed In the example I have provided, the application will not crash but will fail to function. This happens even if you manage the connection yourself. – mattm Nov 07 '17 at 17:15
  • Then what will be a perfect solution? On a separate note: EnableAutoManage doesn't seem to be stable either. – Ahmed Nov 07 '17 at 17:50