【问题标题】:How to stop calling onConnected while activity resume(Android)如何在活动恢复时停止调用 onConnected(Android)
【发布时间】:2016-11-04 09:27:25
【问题描述】:

我有 2 个活动,活动 1 和活动 2。 在 Activity1->onCreate 我使用 googleApliClient 进行定位服务

if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .enableAutoManage(this, this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
        }
@Override
public void onConnected(@Nullable Bundle bundle) {
    createLocationRequest();
}

它工作正常并适当地调用 onConnected,我的问题是当我从 Activity1 移动到 Activity2 并再次回到 Activity1 时,它再次调用 onConnected 方法,我需要停下来

所以我尝试在 Activity1->onPause 上使用以下代码,但没有帮助。

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

【问题讨论】:

  • 在 onCreate、onStart 或 onResume 中哪里创建 GoogleApiClient 对象?
  • 请把onResume方法的代码...
  • 1.GoogleApiClient对象在onCreate中初始化2.onResume方法不在activity中

标签: android google-api-client location-services


【解决方案1】:

Android 中的 Activity 在其自己的生命周期中没有 OnConnected 回调。

Activity 类提供了一组核心的六个回调:onCreate()、onStart()、onResume()、onPause()、onStop() 和 onDestroy()。

按照下图:

请参考:The activity lifecycle

更新以下建议:WhatsThePoint User

【讨论】:

  • 虽然此链接可能有助于您回答问题,但您可以通过获取链接的重要部分并将其放入您的答案来改进此答案,这样可以确保您的答案仍然是一个答案,如果链接被更改或删除:)
猜你喜欢
  • 2014-05-02
  • 1970-01-01
  • 2014-12-10
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 2010-10-17
  • 2015-07-24
  • 2017-02-10
相关资源
最近更新 更多