2016-11-04 40 views
0

我有2个Activity1和Activity2活动。 上Activity1->的onCreate我用googleApliClient位置服务如何在活动恢复时停止拨打onConnected(Android)

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适当的,我的问题是,当我移动到活性2从活动1,再回来到活动1,它再次调用onConnected方法,我需要停止

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

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

凡的onCreate,在onStart或创建的onResume'GoogleApiClient'对象? –

+0

请将onResume方法的代码... –

+0

1.GoogleApiClient对象在onCreate中初始化 2.onResume方法不在活动内 –

回答

0

Android在自己的生命周期中的活动没有OnConnected回调。 onCreate(),onStart(),onResume(),onPause(),onStop()和onDestroy()。Activity类提供了六个回调的核心集合。

按照下图: enter image description here

请参考:The activity lifecycle

更新以下的sugestion:WhatsThePoint User

+0

虽然此链接可能有助于您对问题的回答,但您可以通过将链接的重要部分放入您的答案来改善此答案,这可确保您的答案在链接被更改或删除时仍然是答案:) – WhatsThePoint