How do you correctly close an activity when the user declines to update Google play services? I am using makeGooglePlayServicesAvailable()
because it seems convenient, but I have not found many examples of its use.
I use checkGooglePlayServices()
(code below) in onCreate()
and onResume()
.
public class MainScreen extends Activity {
private static final String TAG = "MainScreen";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkGooglePlayServices();
}
@Override
protected void onResume() {
super.onResume();
checkGooglePlayServices();
}
void checkGooglePlayServices()
{
GoogleApiAvailability.getInstance()
.makeGooglePlayServicesAvailable(this)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void ignored) {
Log.d(TAG,"makeGooglePlayServicesAvailable().onSuccess()");
// GPS available; do something useful
}
}).addOnFailureListener(this,new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG,"makeGooglePlayServicesAvailable().onFailure()");
e.printStackTrace();
Toast.makeText(MainScreen.this,
"Google Play services upgrade required",
Toast.LENGTH_SHORT).show();
// can't proceed without GPS; quit
MainScreen.this.finish(); // this causes a crash
}
});
}
}
The app crashes when finish()
is called:
com.example E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example, PID: 5336
java.lang.RuntimeException: Unable to destroy activity {com.example/com.example.MainScreen}: java.lang.IllegalStateException: Task is already complete
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4438)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4456)
...
Caused by: java.lang.IllegalStateException: Task is already complete
at com.google.android.gms.common.internal.zzbo.zza(Unknown Source:8)
at com.google.android.gms.tasks.zzn.zzDH(Unknown Source:8)
at com.google.android.gms.tasks.zzn.setException(Unknown Source:9)
at com.google.android.gms.tasks.TaskCompletionSource.setException(Unknown Source:2)
...