为什么GoogleApiClient会开始其他活动?

时间:2015-03-15 03:13:11

标签: android android-activity google-api-client

我有以下代码,它是LoginActivity的一部分,它不是MAIN或LAUNCHER活动。此活动从Application Class启动。

问题是,在我按下“登录”按钮后,弹出我选择帐户的对话框,我选择一个,按OK按钮,然后调用我的onStop方法并显示MAIN活动。之后似乎没有其他方法(包括OnConnected)被调用。

这是GoogleApiClient的限制,我只能在主要活动中使用它吗?我还没有找到任何与此有关的内容,我尝试将活动更改为FragmentActivity而没有运气......

@EActivity(R.layout.activity_login)
public class LoginActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

@App
MyApp app;

@ViewById(R.id.layout_login)
LinearLayout layoutLogin;

@ViewById(R.id.layout_loading)
RelativeLayout layoutLoading;

@ViewById(R.id.layout_error)
RelativeLayout layoutError;

private static final int RC_SIGN_IN = 0;

private GoogleApiClient mGoogleApiClient;

private boolean mIntentInProgress;
private boolean mSignInClicked;

private ConnectionResult mConnectionResult;

private void resolveSignInError() {
    if (mConnectionResult.hasResolution()) {
        try {
            mIntentInProgress = true;
            mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
        } catch (IntentSender.SendIntentException e) {
            mIntentInProgress = false;
            mGoogleApiClient.connect();
        }
    }
}

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

@Override
protected void onStop() {
    super.onStop();

    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_PROFILE)
            .build();
}

@Override
public void onConnected(Bundle bundle) {

    layoutLoading.setVisibility(View.VISIBLE);
    layoutLogin.setVisibility(View.GONE);

    mSignInClicked = false;

}

@Override
public void onConnectionSuspended(int cause) {
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    if (!mIntentInProgress) {
        mConnectionResult = result;

        if (mSignInClicked) {
            resolveSignInError();
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
    if (requestCode == RC_SIGN_IN) {
        if (responseCode != RESULT_OK) {
            mSignInClicked = false;
        }

        mIntentInProgress = false;

        if (!mGoogleApiClient.isConnecting()) {
            mGoogleApiClient.connect();
        }
    }
}

@Click(R.id.button_sign_in)
public void signInButtonClicked() {
    if (!mGoogleApiClient.isConnecting()) {
        mSignInClicked = true;
        resolveSignInError();
    }
}

}

1 个答案:

答案 0 :(得分:0)

似乎在AndroidManifest中设置此结果,GoogleApiClient会关闭它所在的活动(如果它不是主要活动)。

android:noHistory="true"