Calling Google Play Game Services from a fragment

后端 未结 4 1615
悲哀的现实
悲哀的现实 2020-12-11 09:19

I have implemented some Google Play Game Services features in my Android app as a separate Activity and I am now trying to rewrite my code as an (Action Bar Sherlock) fragme

4条回答
  •  孤街浪徒
    2020-12-11 09:28

    I'm not sure how similar the API for Games is compared to Nearby (I think this should work), but when creating a GoogleApiClient for Nearby, I found that the objects passed in error callbacks (Status, ConnectionResult) are conveniently Parcelable. So I created an Activity that can be launched to handle these errors, and then pass whatever result it receives back to the caller.

    https://gist.github.com/damien5314/c13ce47bca035c517dfbdfb2af488a73

    Usage:

    Nearby.Messages.publish(mNearbyApi, mMessage)
            .setResultCallback(new ResultCallback() {
                @Override
                public void onResult(@NonNull Status status) {
                    Log.d(TAG, "Nearby publish result: " + getStatusCodeString(status.getStatusCode()));
                    if (status.getStatusCode() > 0) {
                        Intent intent = ResolveErrorActivity.buildIntent(getContext(), status);
                        startActivityForResult(intent, ResolveErrorActivity.REQUEST_RESOLVE_ERROR);
                    }
                }
            });
    

    As long as you call startActivityForResult from a Fragment, it should get the result as usual.

提交回复
热议问题