2014-11-23 129 views
5

我正在使用启动页面来“用Google登录”。当用户有多个帐户...他们选择要登录的帐户后,我试图启动应用程序的主要活动,但由于某种原因,onActivityResult永远不会在我的片段中调用。在startIntentSenderForResult后未调用OnActivityResult

我活动,我打电话onActivityResult,让它叫超级,使片段可以处理它,但它永远不会触发。

有什么建议吗?

这里是有问题的片段:

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); 
    } 
} 
+0

嗨,我有同样的问题...你找到了一些解决方案?将很高兴知道它;-) – 2015-05-24 21:36:11

+1

你在活动的'onActivityResult'上打了个超级电话吗? – ENG618 2015-05-30 00:22:58

+0

非常感谢! 我在我的片段中的活动结果被过分纠缠,但忘了将它写在actvivity .... – 2015-05-31 09:37:11

回答

3

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

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

妥善地解决这一问题,您可以使用早已准备好的解决方案accountPicker
但是 - 如果你想设计自己的解决方案 - 一个想法是用(显然,从第一个活动)开始你的第二个活动:

Intent intent = this.getIntent(); 
intent.putExtra(... /* some code that will control second activity */ 
startActivityForResult(intent, request_code); 

,然后当活动做了所有它不得不这样做你允许它返回:

Intent intent = this.getIntent(); 
intent.putExtra(... /* all needed results to return */); 
this.setResult(RESULT_OK, intent); 
finish(); 

最后它是您的第一个活动,然后获取结果onActivityResult。

1

在你的外在活动添加此:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.change_to_your_id); 
    fragment.onActivityResult(requestCode, resultCode, data); 
} 
+0

这工作,但是我必须用我的实际片段类替换'片段'。 – lenooh 2016-09-04 21:41:16

相关问题