Google PlacePicker UI widget call fails for few (4-5) initial try after app restart but works fine there after
<p>I am trying to implement google PlacePicker widget in my app. What I have been facing is that, after restarting my app and clicking the button to start PlacePicker widget, it fails with <code>Unfortunately, Google Play services has stopped.</code> message for few (4-5) times and then works fine. I have gone through almost every stackoverflow thread related to Google Places API but all of them point to API_KEY issue or Places API for Android being disabled. None of them talked the behavior I am seeing.   </p>

<p>Can someone please suggest what could be the issue. Following are my codes from different files. I am testing on Samsung Galaxy S5.</p>

<p>AndroidManifest.xml</p>

<pre><code>&lt;meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="MY-API-KEY"/&gt;
</code></pre>

<p>MainActivity.java</p>

<pre><code>public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
    static final int PLACE_PICKER_REQUEST = 2;
    public ListView listView;
    private Activity mainActivity = this;

    private static final int REQUEST_RESOLVE_ERROR = 1001;
    // Unique tag for the error dialog fragment
    private static final String DIALOG_ERROR = "dialog_error";
    // Bool to track whether the app is already resolving an error
    private boolean mResolvingError = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          .........
          .........
        setContentView(R.layout.main_activity);
          .........
          .........
        setGoogleApiClient();
        listView = (ListView) findViewById(R.id.shopLV);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {


                if (mGoogleApiClient == null || !mGoogleApiClient.isConnected()) {
                    Toast.makeText(mainActivity, "Enable Location Services or Check your internet connection.", Toast.LENGTH_SHORT).show();
                    return;
                }

                PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();

                try {
                        startActivityForResult(builder.build(mainActivity), PLACE_PICKER_REQUEST);
                } catch (GooglePlayServicesRepairableException
                        | GooglePlayServicesNotAvailableException e) {

                    e.printStackTrace();
                    Toast.makeText(mainActivity, "Google Play Services Unavailable......", Toast.LENGTH_LONG).show();
                }

            }
        });

    }


    private void setGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient
                .Builder(this)
                .enableAutoManage(this, 0, this)
                .addApi(Places.GEO_DATA_API)
                .addApi(Places.PLACE_DETECTION_API)
                .addOnConnectionFailedListener(this)
                .build();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        //If the result is from Google API for Places
        if (requestCode == PLACE_PICKER_REQUEST) {
            processPlacePickerRequest(requestCode, resultCode, data);
        }
    }

private void processPlacePickerRequest(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        Place place = PlacePicker.getPlace(mainActivity, data);
        storeName = place.getName().toString();
        storeAddress = place.getAddress().toString();
        googlePlaceID = place.getId().toString();

    } else {
        Toast.makeText(mainActivity, "Please select again!!", Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult result) {
    if (mResolvingError) {
        // Already attempting to resolve an error.
        return;
    } else if (result.hasResolution()) {
        try {
            mResolvingError = true;
            result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
        } catch (IntentSender.SendIntentException e) {
            // There was an error with the resolution intent. Try again.
            mGoogleApiClient.connect();
        }
    } else {
        // Show dialog using GoogleApiAvailability.getErrorDialog()
        showErrorDialog(result.getErrorCode());
        mResolvingError = true;
    }
}

/*New implemenaton for handling error on connecting to Google API Client*/
// The rest of this code is all about building the error dialog

/* Creates a dialog for an error message */
private void showErrorDialog(int errorCode) {
    // Create a fragment for the error dialog
    ErrorDialogFragment dialogFragment = new ErrorDialogFragment();
    // Pass the error that should be displayed
    Bundle args = new Bundle();
    args.putInt(DIALOG_ERROR, errorCode);
    dialogFragment.setArguments(args);
    //dialogFragment.show(getSupportFragmentManager(), "errordialog");
    dialogFragment.show(getFragmentManager(), "errordialog");
}

/* Called from ErrorDialogFragment when the dialog is dismissed. */
public void onDialogDismissed() {
    mResolvingError = false;
}

/* A fragment to display an error dialog */
public static class ErrorDialogFragment extends DialogFragment {
    public ErrorDialogFragment() { }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Get the error code and retrieve the appropriate dialog
        int errorCode = this.getArguments().getInt(DIALOG_ERROR);
        return GoogleApiAvailability.getInstance().getErrorDialog(
                this.getActivity(), errorCode, REQUEST_RESOLVE_ERROR);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        ((ShoppingListsMainActivity) getActivity()).onDialogDismissed();
    }
}

@Override
protected void onStart() {
    super.onStart();
    if (mGoogleApiClient != null)
        mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    if (mGoogleApiClient != null &amp;&amp; mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
    super.onStop();
}
</code></pre>

<p>In the code above,  I have tried to trim down the code segments not relevant to <code>Google Places API</code>. Please let me know if you feel that I am missing something in the code above and I will clarify. Your help is much appreciated. </p>