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

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

问题描述

有另一种方式来连接谷歌API客户端?

Is there another way to connect Google API client?

我用自动完成的地方,我都用这个code在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()我有很多问题,铸造

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 工作的自动管理。因此最简单的解决方案是增加扩展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 行,并确保您连接 / 断开从自己的客户端。要做到这一点,最常见的地方是 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天全站免登陆