i'm using GoogleApiClient in my android app and experiencing a crash when my app is resuming.
The code looks like this:
MainActivity signature:
public class MainActivity : AppCompatActivity, GoogleApiClient.IOnConnectionFailedListener, Android.Gms.Location.ILocationListener
OnCreate
protected override async void OnCreate(Bundle bundle)
{
App.Initialize();
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
await InitializeGoogleApis();
}
Google API Initialization
private async Task InitializeGoogleApis()
{
// Construct api client and request for required api's
googleApiClientBuilder = new GoogleApiClient.Builder(this)
.AddApi(LocationServices.API)
.EnableAutoManage(this, this);
// Connect to google play services
googleApiClient = await googleApiClientBuilder
.BuildAndConnectAsync();
// Request location api and set common properties
googleLocationRequest = new LocationRequest()
.SetPriority(LocationRequest.PriorityHighAccuracy)
.SetInterval(1000)
.SetFastestInterval(1000);
// Set location changed listener
await LocationServices.FusedLocationApi.RequestLocationUpdatesAsync(googleApiClient, googleLocationRequest, this);
}
OnResume and OnDestroy:
protected override void OnResume()
{
base.OnResume();
}
protected override async void OnDestroy()
{
base.OnDestroy();
if (googleApiClient != null)
await LocationServices.FusedLocationApi.RemoveLocationUpdatesAsync(googleApiClient, this);
}
I have all exceptions turned on but there is no exception description
The app is always crashing when I try to resume. When it crashes, it's put in the background and when I try to resume it again it works perfectly.
Ok I found a hacky way around this, I left all of my implementation the same, just added these functions to my main activity
protected override void OnStop()
{
base.OnStop();
if (googleApiClient != null)
{
LocationServices.FusedLocationApi.Dispose();
googleApiClient.StopAutoManage(this);
googleApiClient.Disconnect();
googleApiClient.Dispose();
googleApiClient = null;
}
}
protected override async void OnRestart()
{
base.OnRestart();
await InitializeGoogleApis();
}
I forcebly stop GoogleApiClient and reinitialize it again inside my OnRestart function. So basicaly that solves my problem.