Android google+ login in a fragment
android, android-fragments, google-plus
Solution
In order to use Fragments with the official tutorial, you have to know that the onActivityResult method will be called within the Activity owning the Fragment.
in the fragment then implement the public `connect()` method, which you call within the activity's onActivityResult instead of calling `plus.connect();` directly. This way turotial will work with using fragments, exactly as intended to work when using Activity. If you need detailed example I can add the edit to this answer.
I would suggest using Activity and not Fragment due to code modularity and easier implementation and handling the on key back buttons to prevent going into the app, etc.
Problem
Currently I am trying to implement google+ login using fragment so I can use it from different activities. I have created fragment like this ``` public class GoogleSignUpFragment extends Fragment implements ConnectionCallbacks, OnConnectionFailedListener, OnClickListener { // PlusClient Variables private static final int REQUEST_CODE_RESOLVE_ERR = 9000; private ProgressDialog mConnectionProgressDialog; private PlusClient mPlusClient; private ConnectionResult mConnectionResult; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); mPlusClient = new PlusClient.Builder(getActivity() .getApplicationContext(), this, this) .setActions("http://schemas.google.com/AddActivity", "http://schemas.google.com/BuyActivity") .setScopes(Scopes.PLUS_LOGIN, Scopes.PLUS_PROFILE).build(); getActivity().findViewById(R.id.sign_in_button) .setOnClickListener(this); // Progress bar to be displayed if the connection failure is not // resolved. mConnectionProgressDialog = new ProgressDialog(getActivity()); mConnectionProgressDialog.setMessage("Signing in..."); } @Override public void onStart() { // TODO Auto-generated method stub super.onStart(); mPlusClient.connect(); } @Override public void onStop() { // TODO Auto-generated method stub super.onStop(); mPlusClient.disconnect(); } @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { if (requestCode == REQUEST_CODE_RESOLVE_ERR && resultCode == Activity.RESULT_OK) { mConnectionResult = null; mPlusClient.connect(); } } @Override public void onClick(View view) { if (view.getId() == R.id.sign_in_button && !mPlusClient.isConnected()) { if (mConnectionResult == null) { mConnectionProgressDialog.show(); } else { try { mConnectionResult.startResolutionForResult(getActivity(), REQUEST_CODE_RESOLVE_ERR); } catch (SendIntentException e) { // Try connecting again. mConnectionResult = null; mPlusClient.connect(); } } } } @Override public void onConnectionFailed(ConnectionResult result) { if (mConnectionProgressDialog.isShowing()) { // The user clicked the sign-in button already. Start to resolve // connection errors. Wait until onConnected() to dismiss the // connection dialog. if (result.hasResolution()) { try { result.startResolutionForResult(getActivity(), REQUEST_CODE_RESOLVE_ERR); } catch (SendIntentException e) { mPlusClient.connect(); } } } } @Override public void onConnected(Bundle connectionHint) { mConnectionProgressDialog.dismiss(); Toast.makeText(getActivity().getApplicationContext(), "User is connected!", Toast.LENGTH_LONG).show(); Intent intent = new Intent(getActivity().getApplicationContext(), LogoutActivity.class); startActivity(intent); } @Override public void onDisconnected() { // TODO Auto-generated method stub } } ``` I was doing it with the official tutorial, and then reworking a little to use it inside fragment. In main activity I have: ``` FragmentManager fragmentManager = getSupportFragmentManager(); android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); googleSignUpFragment = new GoogleSignUpFragment(); fragmentTransaction.add(R.id.detailFragment, googleSignUpFragment); fragmentTransaction.commit(); ``` However whenever I click the log-in button, there is only dialog that says "Signing in..." and nothing else happens. Any ideas how to fix that problem? It was working just fine inside normal activity. Thanks for any help. @Edit. I have figured out that the onConnectionFailed method is not launched, and so there is no onActivityResult it seems, however when I put directly mPlusClient.connect() inside my click it works, but I feel like its not a proper way to do that. Maybe the problem is about that onConnectionFailed not beign launched @Edit 2. Now I have figured out that the problem seems to be with dialog.isShowing() as, when I remove the line, account chooser is showing, right after start, tried with bool value, that I change after click however this does not work too...