在片段中使用 enableAutoManage() [英] using enableAutoManage() in fragment

查看:26
本文介绍了在片段中使用 enableAutoManage()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有其他方式连接 Google API 客户端?

Is there another way to connect Google API client?

我使用自动完成的地方,我必须在 MYFRAGMENT 的某些地方使用此代码

I use auto complete places and I have to use this code some where in MYFRAGMENT

mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
                .addApi(Places.GEO_DATA_API)
                .enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
                .addConnectionCallbacks(this).build();

我的问题

enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
                    .addConnectionCallbacks(this).build();

我无法处理它,因为当我用 getActivity() 替换 this 时,我在转换时遇到了很多问题

I can't deal with it because when I replace this with getActivity() I have many problem with casting

感谢您的帮助,如果这个问题很愚蠢,请见谅.

thanks for help and sorry if this question is silly.

推荐答案

如果您想使用 enableAutoManage,那么您必须使您的活动扩展 FragmentActivity.GoogleApiClient 的自动管理需要它进行回调才能工作.因此,最简单的解决方案是将 extends FragmentActivity 添加到您的活动中.这样您的转换就不会失败并导致应用在运行时崩溃.

If you want to use enableAutoManage then you must make your activity extend FragmentActivity. The callbacks it makes are required for the automatic management of the GoogleApiClient to work. So the easiest solution is to add extends FragmentActivity to your activity. Then your cast would not fail and cause the app to crash at runtime.

另一种解决方案是自己管理 api 客户端.您将从构建器中删除 enableAutoManage 行,并确保您自己从客户端connect/disconnect.执行此操作的最常见位置是 onStart()/onStop().像……

The alternate solution is to manage the api client yourself. You would remove the enableAutoManage line from the builder, and make sure you connect/disconnect from the client yourself. The most common place to do this is onStart()/onStop(). Something like...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
            .addApi(Places.GEO_DATA_API)
            .addConnectionCallbacks(this).build();
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();
}

这篇关于在片段中使用 enableAutoManage()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆