rtIntentSenderForResult后的onActiv

rtIntentSenderForResult后的onActiv

本文介绍了startIntentSenderForResult后的onActivityResult不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是启动页为在与谷歌。当用户有多个帐户......他们选择哪个帐户后,他们希望着,我试图启动应用程序主要活动登录,但由于某些原因,的onActivityResult 从未被称为在我的片段。

林活动,我称之为的onActivityResult ,并让它调用super,这样的片段可以处理它,但它永远不会触发。

有什么建议?

下面是有问题的片段:

 包com.garciaericn.goodeats.login;进口android.app.Activity;
进口android.app.Fragment;
进口android.content.Intent;
进口android.content.IntentSender;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.Toast;进口com.garciaericn.goodeats.R;
进口com.garciaericn.goodeats.helpers.CheckConnection;
进口com.garciaericn.goodeats.main.FavoritesActivity;
进口com.google.android.gms.common.ConnectionResult;
进口com.google.android.gms.common.SignInButton;
进口com.google.android.gms.common.api.GoogleApiClient;
进口com.google.android.gms.plus.Plus;公共类LoginFragment扩展片段实现View.OnClickListener,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {    公共静态最后的字符串标记=com.garciaericn.goodeats.login.LoginFragment.TAG;    / *请求code使用的用户交互来调用迹象。 * /
    私有静态最终诠释RC_SIGN_IN = 0;
    私有静态最终诠释RC_LOGGED_IN = 1034553;
    公共静态最终诠释RC_SIGN_OUT = 34458392;
    / *客户端用来与谷歌API的交互。 * /
    私人GoogleApiClient mGoogleApiClient;
    / *一个标志,指示一个的PendingIntent正在进行和prevents
     *美国启动进一步的意图。
     * /
    私人布尔mIntentInProgress;
    / *存储从onConnectionFailed回调连接的结果使我们能
     *解决这些问题,当用户点击登录。
     * /
    私人ConnectionResult mConnectionResult;
    私人布尔mSignInClicked;
    私人布尔mSignedIn;
    私人CheckConnection checkConnection;    公共LoginFragment(){
    }    公共静态LoginFragment的getInstance(){
        返回新LoginFragment();
    }    私人无效signOut(){
        如果(mGoogleApiClient.isConnected()){
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            mGoogleApiClient.disconnect();
            mGoogleApiClient.connect();
            mIntentInProgress = FALSE;
            mSignInClicked = FALSE;
        }
    }    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        checkConnection =新CheckConnection(getActivity());
        setHasOptionsMenu(真);        mGoogleApiClient =新GoogleApiClient.Builder(getActivity())
                .addConnectionCallbacks(本)
                .addOnConnectionFailedListener(本)
                .addApi(Plus.API)
                .addScope(Plus.SCOPE_PLUS_LOGIN)
                。建立();
    }    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
        查看查看= inflater.inflate(R.layout.fragment_login,集装箱,FALSE);        SignInButton signInButton =(SignInButton)view.findViewById(R.id.g_plus_login);
        signInButton.setSize(SignInButton.SIZE_WIDE);
        signInButton.setOnClickListener(本);        返回视图。
    }    @覆盖
    公共无效调用onStart(){
        super.onStart();
        mGoogleApiClient.connect();
    }    @覆盖
    公共无效的onStop(){
        super.onStop();
        如果(mGoogleApiClient.isConnected()){
            mGoogleApiClient.disconnect();
        }
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){
            案例R.id.action_sign_out:
                如果(mGoogleApiClient.isConnected()){
                    Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
                    mGoogleApiClient.disconnect();
                    mGoogleApiClient.connect();
                }
                返回true;
        }        返回super.onOptionsItemSelected(项目);
    }    @覆盖
    公共无效的onClick(视图v){
        checkConnection.isConnected();
        开关(v.getId()){
            案例R.id.g_plus_login:
                如果(!mGoogleApiClient.isConnected()){
                    mSignInClicked = TRUE;
                    resolveSignInError();
                }
                打破;
            默认:
                //如果需要默认操作。
                打破;
        }
    }    @覆盖
    公共无效onConnected(束束){
        mSignInClicked = FALSE;
        mSignedIn =真;
        //用户连接
        字符串帐户名= Plus.AccountApi.getAccountName(mGoogleApiClient);
        Toast.makeText(getActivity(),帐户名,Toast.LENGTH_SHORT).show();
        //串帐户ID = GoogleAuthUtil.getAccountId(getActivity(),帐户名);
        //尝试{
        //帐户ID = GoogleAuthUtil.getAccountId(getActivity()getApplicationContext(),帐户名。);
        //}赶上(GoogleAuthException E){
        // e.printStackTrace();
        //}赶上(IOException异常五){
        // e.printStackTrace();
        //}        //如果(帐户!= NULL){
        // // TODO:createLocalAccount()= Store帐户名称和ID与餐厅DB
        //}
        //启动主要活动
        意向意图=新意图(getActivity(),FavoritesActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(意向);
    }    @覆盖
    公共无效onConnectionSuspended(int i)以{
        mGoogleApiClient.connect();
    }    @覆盖
    公共无效onConnectionFailed(ConnectionResult connectionResult){
        如果(!mIntentInProgress){
            //存放ConnectionResult这样我们就可以在以后使用它的用户点击时,
            // '签到'。
            mConnectionResult = connectionResult;            如果(mSignInClicked){
                resolveSignInError();
            }        }
        checkConnection.isConnected();
        //如果(!checkConnection.isConnected()){
        // Toast.makeText(getActivity(),无网络连接,Toast.LENGTH_SHORT).show();
        //}    }    公共无效resolveSignInError(){
        如果(mConnectionResult.hasResolution()){
            尝试{
                mIntentInProgress =真;
                。getActivity()startIntentSenderForResult(mConnectionResult.getResolution()getIntentSender(),RC_SIGN_IN,NULL,0,0,0);
            }赶上(IntentSender.SendIntentException E){
                //它被发送之前的意图被取消。返回到默认
                //状态,并尝试连接到获得更新ConnectionResult。
                mIntentInProgress = FALSE;
                mGoogleApiClient.connect();
            }
        }
    }    @覆盖
    公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
        如果(要求code == RC_SIGN_IN){
            如果(结果code!= Activity.RESULT_OK){
                mSignInClicked = FALSE;
            }            mIntentInProgress = FALSE;            如果(!mGoogleApiClient.isConnected()){
                mGoogleApiClient.connect();
            }
        }否则如果(要求code == RC_LOGGED_IN){
            如果(结果code == RC_SIGN_OUT){
                登出();
            }
        }
        super.onActivityResult(要求code,结果code,数据);
    }
}


解决方案

关键是要调用的第一个活动下面的方法。

  @覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    super.onActivityResult(要求code,结果code,数据);
}

I'm using a launch page to "Sign in with Google". When the user has multiple accounts...after they select which account they wish to sign in with, I'm trying to launch the apps main activity, but for some reason the onActivityResult is never called in my fragment.

Im the Activity, I call onActivityResult and let it call super so that the fragment can handle it, but it never fires.

Any suggestions?

Here is the fragment that is in question:

package com.garciaericn.goodeats.login;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.garciaericn.goodeats.R;
import com.garciaericn.goodeats.helpers.CheckConnection;
import com.garciaericn.goodeats.main.FavoritesActivity;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.plus.Plus;

public class LoginFragment extends Fragment implements View.OnClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    public static final String TAG = "com.garciaericn.goodeats.login.LoginFragment.TAG";

    /* Request code used to invoke sign in user interactions. */
    private static final int RC_SIGN_IN = 0;
    private static final int RC_LOGGED_IN = 1034553;
    public static final int RC_SIGN_OUT = 34458392;
    /* Client used to interact with Google APIs. */
    private GoogleApiClient mGoogleApiClient;
    /* A flag indicating that a PendingIntent is in progress and prevents
     * us from starting further intents.
     */
    private boolean mIntentInProgress;
    /* Store the connection result from onConnectionFailed callbacks so that we can
     * resolve them when the user clicks sign-in.
     */
    private ConnectionResult mConnectionResult;
    private boolean mSignInClicked;
    private boolean mSignedIn;
    private CheckConnection checkConnection;

    public LoginFragment() {
    }

    public static LoginFragment getInstance() {
        return new LoginFragment();
    }

    private void signOut() {
        if (mGoogleApiClient.isConnected()) {
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            mGoogleApiClient.disconnect();
            mGoogleApiClient.connect();
            mIntentInProgress = false;
            mSignInClicked = false;
        }
    }

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

        checkConnection = new CheckConnection(getActivity());
        setHasOptionsMenu(true);

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_login, container, false);

        SignInButton signInButton = (SignInButton) view.findViewById(R.id.g_plus_login);
        signInButton.setSize(SignInButton.SIZE_WIDE);
        signInButton.setOnClickListener(this);

        return view;
    }

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

    @Override
    public void onStop() {
        super.onStop();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_sign_out:
                if (mGoogleApiClient.isConnected()) {
                    Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
                    mGoogleApiClient.disconnect();
                    mGoogleApiClient.connect();
                }
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        checkConnection.isConnected();
        switch (v.getId()) {
            case R.id.g_plus_login:
                if (!mGoogleApiClient.isConnected()) {
                    mSignInClicked = true;
                    resolveSignInError();
                }
                break;
            default:
                // If default action is needed.
                break;
        }
    }

    @Override
    public void onConnected(Bundle bundle) {
        mSignInClicked = false;
        mSignedIn = true;
        // User is connected


        String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient);
        Toast.makeText(getActivity(), accountName, Toast.LENGTH_SHORT).show();
        //        String accountID = GoogleAuthUtil.getAccountId(getActivity(), accountName);
        //        try {
        //            accountID = GoogleAuthUtil.getAccountId(getActivity().getApplicationContext(),accountName);
        //        } catch (GoogleAuthException e) {
        //            e.printStackTrace();
        //        } catch (IOException e) {
        //            e.printStackTrace();
        //        }

        //        if (accountID != null) {
        //            // TODO: createLocalAccount() = Store account name and id with DB of restaurants
        //        }


        // Launch main activity
        Intent intent = new Intent(getActivity(), FavoritesActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

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

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (!mIntentInProgress) {
            // Store the ConnectionResult so that we can use it later when the user clicks
            // 'sign-in'.
            mConnectionResult = connectionResult;

            if (mSignInClicked) {
                resolveSignInError();
            }

        }
        checkConnection.isConnected();
        //        if (!checkConnection.isConnected()) {
        //            Toast.makeText(getActivity(), "No network connection", Toast.LENGTH_SHORT).show();
        //        }

    }

    public void resolveSignInError() {
        if (mConnectionResult.hasResolution()) {
            try {
                mIntentInProgress = true;
                getActivity().startIntentSenderForResult(mConnectionResult.getResolution().getIntentSender(), RC_SIGN_IN, null, 0, 0, 0);
            } catch (IntentSender.SendIntentException e) {
                // The intent was canceled before it was sent.  Return to the default
                // state and attempt to connect to get an updated ConnectionResult.
                mIntentInProgress = false;
                mGoogleApiClient.connect();
            }
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RC_SIGN_IN) {
            if (resultCode != Activity.RESULT_OK) {
                mSignInClicked = false;
            }

            mIntentInProgress = false;

            if (!mGoogleApiClient.isConnected()) {
                mGoogleApiClient.connect();
            }
        } else if (requestCode == RC_LOGGED_IN) {
            if (resultCode == RC_SIGN_OUT) {
                signOut();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
解决方案

The key was to call the below method in the first activity.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}

这篇关于startIntentSenderForResult后的onActivityResult不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:30