我尝试按照Google登录的片段进行操作,但不是将其放在活动上,而是在片段上使用它。它只适用于牛轧糖。问题似乎依赖于给予谷歌的意图。每当我点击Google中的标志时,它都会关闭我的应用。
对话框出现在我的应用程序之外,并且它没有回到我的片段。它只是关闭了。
所以这是我的代码:它与代码段几乎相同。但问题是,当我点击Google登录时应用程序关闭时,它允许用户每次都选择电子邮件,但它不会返回到onActivityResult,因为我的logcat上没有记录数据
public class LoginFragment extends Fragment implements OnConnectionFailedListener {
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
private Button signUp, login;
private View view;
private SharedPref pref;
private SignInButton signInButton;
private GoogleSignInOptions gso;
private GoogleApiClient mGoogleApiClient;
private int RC_SIGN_IN = 0;
private static final String TAG = "GPlusFragment";
private Retrofit retrofit;
private RequestInterface requestInterface;
private ServerRequest request;
private boolean connectionError = false;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestProfile()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.enableAutoManage(getActivity() /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.login_layout, container, false);
signUp = (Button) view.findViewById(R.id.signup);
login = (Button) view.findViewById(R.id.login);
fragmentManager = getFragmentManager();
//Initializing signinbutton
signInButton = (SignInButton) view.findViewById(R.id.google_sign_in_button);
signInButton.setSize(SignInButton.SIZE_WIDE);
return view;
}
@Override
public void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
mGoogleApiClient.stopAutoManage(getActivity());
mGoogleApiClient.disconnect();
connectionError = true;
signInButton.setVisibility(View.GONE);
}
@Override
public void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
@Override
public void onDestroy() {
super.onDestroy();
//mGoogleApiClient.stopAutoManage(getActivity());
mGoogleApiClient.disconnect();
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (connectionError){
mGoogleApiClient.stopAutoManage(getActivity());
mGoogleApiClient.disconnect();
signInButton.setVisibility(View.GONE);
}
signUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fragmentTransaction = fragmentManager.beginTransaction().addToBackStack("signup").replace(R.id.content_view, new RegisterFragment());
fragmentTransaction.commit();
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fragmentTransaction = fragmentManager.beginTransaction().addToBackStack("signin").replace(R.id.content_view, new SignInFragment());
fragmentTransaction.commit();
}
});
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Auth.GoogleSignInApi.signOut(mGoogleApiClient);
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("It's here", "here");
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
//my code here doing the login
}
}
public void goToProfile(){
Intent intent = new Intent(getContext(), HomeLoggedInActivity.class);
startActivity(intent);
}
}
答案 0 :(得分:1)
通过删除问题已解决问题 机器人:noHistory ="真"在清单中。之前的解决方案是阻止用户返回登录活动。
答案 1 :(得分:0)
你必须使用你的实际Android手机而不是模拟器,因为它有一个旧版本的谷歌播放