I'm currently struggling with including the Google Play/Plus API in my android app. The problem is, I'm not sure how to implement the API correctly, so that it disconnects when the user closes the app but the he/she doesn't have to log in again when he switches the activity. Not every activity needs the API though. So I HAVE read this but this wasn't too helpful for me.
So first I tried solution 1, I disconnected in the activites' onStop()
and connected in onStart()
again. The main problem was that the started activity's onStart()
was called before the previous activity's onStop()
, so my app connected again while already being connected and then disconnected. Also in my opinion this way of doing this is not that nice.
Continuing with a similar approach like solution 2: I implemented a Fragment
that was supposed to handle the connection stuff while being added to the activities. The problem here seems to be that, unlike my expectations, the Fragment
is instantiated every time one of the activities is started, so it tries to reconnect every time and I couldn't find a lifecycle event that represents the app being closed that differs from the event when the user switches the activity. I thought it would be one Fragment
that would be attached to every necessary activity.
Any detailed advices on how to do this properly, maybe with not too complicated examples?