title: Twitter Login in fragment doesn't work
body: <p>I tried the same code in Activity and it worked but the same code in fragment doesn't work.</p>
  
  <p>SignUpFragment.java:</p>
  
  <pre><code>package com.conversionbug.alltee;
  
  import android.content.Intent;
  import android.graphics.Color;
  import android.graphics.Typeface;
  import android.os.AsyncTask;
  import android.os.Build;
  import android.os.Bundle;
  import android.support.annotation.NonNull;
  import android.support.design.widget.Snackbar;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentTransaction;
  import android.support.v4.content.ContextCompat;
  import android.view.Gravity;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ImageButton;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.facebook.AccessToken;
  import com.facebook.CallbackManager;
  import com.facebook.FacebookCallback;
  import com.facebook.FacebookException;
  import com.facebook.FacebookSdk;
  import com.facebook.GraphRequest;
  import com.facebook.GraphResponse;
  import com.facebook.LoggingBehavior;
  import com.facebook.login.LoginResult;
  import com.facebook.login.widget.LoginButton;
  import com.google.android.gms.auth.api.Auth;
  import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
  import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
  import com.google.android.gms.auth.api.signin.GoogleSignInResult;
  import com.google.android.gms.common.ConnectionResult;
  import com.google.android.gms.common.SignInButton;
  import com.google.android.gms.common.api.GoogleApiClient;
  import com.twitter.sdk.android.core.Callback;
  import com.twitter.sdk.android.core.Result;
  import com.twitter.sdk.android.core.Twitter;
  import com.twitter.sdk.android.core.TwitterAuthToken;
  import com.twitter.sdk.android.core.TwitterCore;
  import com.twitter.sdk.android.core.TwitterException;
  import com.twitter.sdk.android.core.TwitterSession;
  import com.twitter.sdk.android.core.identity.TwitterAuthClient;
  import com.twitter.sdk.android.core.identity.TwitterLoginButton;
  
  import org.json.JSONObject;
  
  import java.util.Arrays;
  
  import okhttp3.FormBody;
  import okhttp3.OkHttpClient;
  import okhttp3.Request;
  import okhttp3.RequestBody;
  import okhttp3.Response;
  
  import static com.conversionbug.alltee.Constants.ROOT_LOGIN;
  import static com.conversionbug.alltee.Constants.ROOT_TOKEN;
  import static com.conversionbug.alltee.Constants.SOCIAL_REGISTER;
  
  import static com.facebook.FacebookSdk.getApplicationContext;
  
  public class SignUpFragment extends Fragment implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {
  
      TextView loginTV;
      Button nextActivity;
      EditText name;
      EditText email;
      EditText password;
      LinearLayout linear_main;
  
      String googleName;
      String googleEmail;
      String googleUserAccessToken;
      String googleUserSocialId;
  
      String facebookEmail;
      String facebookId;
      String facebookName;
      String facebookToken;
  
      ImageButton facebook_icon1;
      LoginButton facebook_sign_in_button1;
      ImageButton google_plus_icon1;
      SignInButton googleSignIn1;
  
      ImageButton twitter_icon;
  
      TwitterLoginButton twitter1;
  
      CallbackManager callbackManager;
      private GoogleApiClient googleApiClient1;
      private static final int REQ_CODE = 9001;
      GoogleSignInOptions signInOptions;
  
      TwitterSession session;
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_sign_up, container, false);
          FacebookSdk.sdkInitialize(getApplicationContext());
  
          callbackManager = CallbackManager.Factory.create();
  
          Twitter.initialize(getActivity()); //Twitter is initialized
  
          loginTV = (TextView) view.findViewById(R.id.loginTV);
          nextActivity = (Button) view.findViewById(R.id.nextActivity);
          name = (EditText) view.findViewById(R.id.name);
          email = (EditText) view.findViewById(R.id.email);
          password = (EditText) view.findViewById(R.id.password);
          linear_main = (LinearLayout) view.findViewById(R.id.linearMain1);
          facebook_icon1 = (ImageButton) view.findViewById(R.id.facebook_icon1);
          facebook_sign_in_button1 = (LoginButton) view.findViewById(R.id.facebook_sign_in_button1);
  
          facebook_sign_in_button1.setReadPermissions(Arrays.asList("email"));
  
          twitter1 = (TwitterLoginButton) view.findViewById(R.id.twitter1);
          twitter_icon = (ImageButton) view.findViewById(R.id.twitter_icon);
  
          google_plus_icon1 = (ImageButton) view.findViewById(R.id.google_plus_icon1);
          googleSignIn1 = (SignInButton) view.findViewById(R.id.googleSignIn1);
          facebook_sign_in_button1.setFragment(this);
  
          signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestIdToken(getString(R.string.default_web_client_id)).requestEmail().build();
          googleApiClient1 = new GoogleApiClient.Builder(getActivity())
                  //.enableAutoManage(getActivity() /* FragmentActivity */, 1, this /* OnConnectionFailedListener */)
                  .addApi(Auth.GOOGLE_SIGN_IN_API, signInOptions)
                  .build();
  
          try {
              session = TwitterCore.getInstance().getSessionManager().getActiveSession();
          } catch (Exception exception) {
              exception.printStackTrace();
          }
  
  
          facebook_icon1.setOnClickListener(this);
          google_plus_icon1.setOnClickListener(this);
          twitter_icon.setOnClickListener(this);
  
  
  
          facebook_sign_in_button1.registerCallback(callbackManager, new FacebookCallback&lt;LoginResult&gt;() {
              @Override
              public void onSuccess(LoginResult loginResult) {
  
                  GraphRequest request = GraphRequest.newMeRequest(
                          loginResult.getAccessToken(),
                          new GraphRequest.GraphJSONObjectCallback() {
                              @Override
                              public void onCompleted(JSONObject object, GraphResponse response) {
  
                                  FacebookSdk.setIsDebugEnabled(true);
                                  FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
  
                                  facebookToken = AccessToken.getCurrentAccessToken().getToken();
  
  
                                  try {
                                      facebookEmail = object.getString("email");
                                      facebookId = object.getString("id");
                                      facebookName = object.getString("name");
  
  
                                      Toast.makeText(getActivity(), "Email: " + facebookEmail + " Id: " + facebookId + " Name: " + facebookName + " Access Token: " + facebookToken, Toast.LENGTH_LONG).show();
                                  } catch (Exception e) {
                                      e.printStackTrace();
                                  }
  
                                  FacebookRegisterTask facebookRegisterTask = new FacebookRegisterTask();
                                  facebookRegisterTask.execute(SOCIAL_REGISTER);
                              }
                          });
                  Bundle parameters = new Bundle();
                  parameters.putString("fields", "email,id,name");
                  request.setParameters(parameters);
                  request.executeAsync();
              }
  
              @Override
              public void onCancel() {
                  Toast.makeText(getApplicationContext(), "Login Cancel", Toast.LENGTH_LONG).show();
              }
  
              @Override
              public void onError(FacebookException e) {
                  Toast.makeText(getApplicationContext(), "Login Error", Toast.LENGTH_LONG).show();
              }
          });
  
          loginTV.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view1) {
                  Fragment fragment = new LoginFragment();
                  FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                  FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                  fragmentTransaction.addToBackStack(null);
                  fragmentTransaction.replace(R.id.container_body, fragment);
                  fragmentTransaction.commit();
              }
          });
  
          nextActivity.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  RegisterTask registerTask = new RegisterTask();
                  registerTask.execute(ROOT_LOGIN);
  
                  String nameValidation = name.getText().toString().trim();
                  String emailValidation = email.getText().toString().trim();
                  String passwordValidation = password.getText().toString().trim();
                  String namePattern = "[A-Za-z. ]+";
                  String emailPattern = "[a-z0-9._]+@[a-z]+\\.+[a-z]+";
                  if (nameValidation.matches(namePattern) &amp;&amp; name.length() &lt;= 255 &amp;&amp; emailValidation.matches(emailPattern) &amp;&amp; passwordValidation.length() &gt;= 6) {
  
                  } else {
                      Snackbar snackbar = Snackbar.make(linear_main, "Wrong Username or Password !!!", Snackbar.LENGTH_LONG);
                      View sbView = snackbar.getView();
                      TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
                      textView.setTextColor(Color.RED);
                      textView.setTypeface(null, Typeface.BOLD);
                      textView.setTextSize(16f);
                      sbView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.red));
                      textView.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
                      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN_MR1)
                          textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
                      else
                          textView.setGravity(Gravity.CENTER_HORIZONTAL);
                      snackbar.show();
                  }
              }
          });
  
          twitter1.setCallback(new Callback&lt;TwitterSession&gt;() {
              @Override
              public void success(Result&lt;TwitterSession&gt; result) {
  
                  TwitterAuthToken authToken = session.getAuthToken();
                  String token = authToken.token;
                  String secret = authToken.secret;
  
                  session = result.data;
                  String userName = session.getUserName().toString();
                  long userId = session.getId();
  
                  if(userName.isEmpty()) {
                      Toast.makeText(getActivity(), "Data isn't fetched", Toast.LENGTH_LONG).show();
                  }
                  else {
                      Toast.makeText(getActivity(), "Login Successful with token: " + token + "\n secret: " + secret + "\n User Name: " + userName + "\n Id: " + userId, Toast.LENGTH_LONG).show();
                  }
  
                  /*getUserData();*/
              }
              @Override
              public void failure(TwitterException exception) {
                  Toast.makeText(getApplicationContext(), "Login Failure", Toast.LENGTH_LONG).show();
              }
          });
          return view;
      }
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          callbackManager.onActivityResult(requestCode, resultCode, data);
          Toast.makeText(getActivity(),"inside onActivityResult",Toast.LENGTH_LONG).show();
  
          if (requestCode == REQ_CODE) {
              GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
              handleResult(result);
          }
          Fragment fragment = getFragmentManager().findFragmentById(R.id.linearMain1);
          if (fragment != null) {
              fragment.onActivityResult(requestCode, resultCode, data);
          }
      }
  
      /*void getUserData() {
          TwitterAuthClient authClient = new TwitterAuthClient();
          authClient.requestEmail(session, new Callback&lt;String&gt;() {
  
              @Override
              public void success(Result&lt;String&gt; result1) {
  
                  String s1 = result1.data.toString().toLowerCase();
                  if(s1.isEmpty()) {
                      Toast.makeText(getActivity(),"No EmailId",Toast.LENGTH_LONG).show();
  
                  }
                  else {
                      Toast.makeText(getActivity(),"Email: "+s1,Toast.LENGTH_LONG).show();
                  }
              }
  
              @Override
              public void failure(TwitterException exception) {
  
              }
          });
      }
  */
  
      @Override
      public void onClick(View view) {
          switch (view.getId()) {
              case R.id.facebook_icon1:
                  facebook_sign_in_button1.performClick();
                  break;
  
              case R.id.google_plus_icon1:
                  signIn();
                  break;
  
              case R.id.twitter_icon:
                  twitter1.performClick();
                  break;
          }
      }
  
      @Override
      public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
          Toast.makeText(getApplicationContext(), "Connection failed G+", Toast.LENGTH_SHORT).show();
      }
  
      private void handleResult(GoogleSignInResult result) {
          if (result.isSuccess()) {
              GoogleSignInAccount account = result.getSignInAccount();
              googleName = account.getDisplayName();
              googleEmail = account.getEmail();
              googleUserAccessToken = account.getIdToken();
              googleUserSocialId = account.getId();
              Toast.makeText(getActivity(), "Name: " + googleName + " Email: " + googleEmail + " UserAccessToken: " + googleUserAccessToken + " UserSocialId " + googleUserSocialId, Toast.LENGTH_SHORT).show();
  
              GoogleRegisterTask googleRegisterTask = new GoogleRegisterTask();
              googleRegisterTask.execute(SOCIAL_REGISTER);
          }
      }
  
      private void signIn() {
          Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient1);
          startActivityForResult(intent, REQ_CODE);
      }
  
      @Override
      public void onStart() {
          super.onStart();
          if (googleApiClient1 != null)
              googleApiClient1.connect();
      }
  
      @Override
      public void onStop() {
          if (googleApiClient1 != null &amp;&amp; googleApiClient1.isConnected()) {
              googleApiClient1.disconnect();
          }
          super.onStop();
      }
  
      public class RegisterTask extends AsyncTask&lt;String, Void, String&gt; {
          int responseCode;
  
          @Override
          protected String doInBackground(String... params) {
              try {
                  OkHttpClient client = new OkHttpClient();
  
                  RequestBody postData = new FormBody.Builder()
                          .add("name", name.getText().toString())
                          .add("email", email.getText().toString())
                          .add("password", password.getText().toString())
                          .build();
  
                  Request request = new Request.Builder()
                          .url(params[0])
                          .post(postData)
                          .build();
  
                  Response response = client.newCall(request).execute();
                  responseCode = response.code();
                  String result = response.body().string();
                  return result;
              } catch (Exception e) {
                  e.printStackTrace();
              }
              return null;
          }
  
          @Override
          protected void onPostExecute(String s) {
              super.onPostExecute(s);
              /*Toast.makeText(getActivity(), ""+s.toString(), Toast.LENGTH_SHORT).show();*/
  
              if (responseCode == 200) {
                  AuthTask authTask = new AuthTask();
                  authTask.execute(ROOT_TOKEN);
  
                  Intent intent = new Intent(getActivity(), BottomNavigationActivity.class);
                  startActivity(intent);
              } else if (s.toString().equals("{\"error\":\"Email id already taken\"}")) {
  
                  Toast.makeText(getActivity(), "Email-id already exist", Toast.LENGTH_LONG).show();
              }
          }
      }
  
      public class AuthTask extends AsyncTask&lt;String, Void, String&gt; {
          int responseCode1;
  
          @Override
          protected String doInBackground(String... params1) {
              try {
                  OkHttpClient client = new OkHttpClient();
  
                  RequestBody postData = new FormBody.Builder()
                          .add("client_id", "2")
                          .add("client_secret", "oreOeeeN7ZYpCGNFMmW2W1OowPQVaJ92jadcl8B2")
                          .add("grant_type", "password")
                          .add("scope", "*")
                          .add("username", email.getText().toString())
                          .add("password", password.getText().toString())
                          .build();
  
                  Request request = new Request.Builder()
                          .url(params1[0])
                          .post(postData)
                          .build();
  
                  Response response = client.newCall(request).execute();
                  responseCode1 = response.code();
                  String result = response.body().string();
  
                  return result;
              } catch (Exception e) {
                  e.printStackTrace();
              }
              return null;
          }
  
          @Override
          protected void onPostExecute(String s) {
              super.onPostExecute(s);
              Toast.makeText(getActivity(), "" + s.toString(), Toast.LENGTH_LONG).show();
          }
      }
  
      public class GoogleRegisterTask extends AsyncTask&lt;String, Void, String&gt; {
          int responseCode;
  
          @Override
          protected String doInBackground(String... params) {
              try {
                  OkHttpClient client = new OkHttpClient();
  
                  RequestBody postData = new FormBody.Builder()
                          .add("name", googleName)
                          .add("email", googleEmail)
                          .add("access_token", googleUserAccessToken)
                          .add("social_id", googleUserSocialId)
                          .add("social", "google_id")
                          .build();
  
                  Request request = new Request.Builder()
                          .url(params[0])
                          .post(postData)
                          .build();
  
                  Response response = client.newCall(request).execute();
                  responseCode = response.code();
                  String result = response.body().string();
                  return result;
              } catch (Exception e) {
                  e.printStackTrace();
              }
              return null;
          }
  
          @Override
          protected void onPostExecute(String s) {
              super.onPostExecute(s);
              if (responseCode == 200) {
                  GoogleAuthTask googleAuthTask = new GoogleAuthTask();
                  googleAuthTask.execute(ROOT_TOKEN);
  
                  Intent intent = new Intent(getActivity(), BottomNavigationActivity.class);
                  startActivity(intent);
              }
          }
      }
  
      public class GoogleAuthTask extends AsyncTask&lt;String, Void, String&gt; {
          int responseCode1;
  
          @Override
          protected String doInBackground(String... params1) {
              try {
                  OkHttpClient client = new OkHttpClient();
  
                  RequestBody postData = new FormBody.Builder()
                          .add("grant_type", "social")
                          .add("client_id", "2")
                          .add("client_secret", "oreOeeeN7ZYpCGNFMmW2W1OowPQVaJ92jadcl8B2")
                          .add("access_token", googleUserAccessToken)
                          .add("network", "google_id")
                          .build();
  
                  Request request = new Request.Builder()
                          .url(params1[0])
                          .post(postData)
                          .build();
  
                  Response response = client.newCall(request).execute();
                  responseCode1 = response.code();
                  String result = response.body().string();
  
                  return result;
              } catch (Exception e) {
                  e.printStackTrace();
              }
              return null;
          }
  
          @Override
          protected void onPostExecute(String s) {
              super.onPostExecute(s);
              Toast.makeText(getActivity(), "" + s.toString(), Toast.LENGTH_LONG).show();
          }
      }
  
      public class FacebookRegisterTask extends AsyncTask&lt;String, Void, String&gt; {
          int responseCode;
  
          @Override
          protected String doInBackground(String... params) {
              try {
                  OkHttpClient client = new OkHttpClient();
  
                  RequestBody postData = new FormBody.Builder()
                          .add("name", facebookName)
                          .add("email", facebookEmail)
                          .add("access_token", facebookToken)
                          .add("social_id", facebookId)
                          .add("social", "facebook_id")
                          .build();
  
                  Request request = new Request.Builder()
                          .url(params[0])
                          .post(postData)
                          .build();
  
                  Response response = client.newCall(request).execute();
                  responseCode = response.code();
                  String result = response.body().string();
                  return result;
              } catch (Exception e) {
                  e.printStackTrace();
              }
              return null;
          }
  
          @Override
          protected void onPostExecute(String s) {
              super.onPostExecute(s);
              FacebookAuthTask facebookAuthTask = new FacebookAuthTask();
              facebookAuthTask.execute(ROOT_TOKEN);
  
              Intent intent = new Intent(getActivity(), BottomNavigationActivity.class);
              startActivity(intent);
              Toast.makeText(getActivity(), "" + responseCode, Toast.LENGTH_LONG).show();
          }
      }
  
      public class FacebookAuthTask extends AsyncTask&lt;String, Void, String&gt; {
          int responseCode1;
  
          @Override
          protected String doInBackground(String... params1) {
              try {
                  OkHttpClient client = new OkHttpClient();
  
                  RequestBody postData = new FormBody.Builder()
                          .add("grant_type", "social")
                          .add("client_id", "2")
                          .add("client_secret", "oreOeeeN7ZYpCGNFMmW2W1OowPQVaJ92jadcl8B2")
                          .add("access_token", facebookToken)
                          .add("network", "facebook_id")
                          .build();
  
                  Request request = new Request.Builder()
                          .url(params1[0])
                          .post(postData)
                          .build();
  
                  Response response = client.newCall(request).execute();
                  responseCode1 = response.code();
                  String result = response.body().string();
  
                  return result;
              } catch (Exception e) {
                  e.printStackTrace();
              }
              return null;
          }
  
          @Override
          protected void onPostExecute(String s) {
              super.onPostExecute(s);
              Toast.makeText(getActivity(), "" + s.toString(), Toast.LENGTH_LONG).show();
          }
      }
  }  }
      }
  }
  </code></pre>
  
  <p>fragment_sign_up.xml:`</p>
  
  <pre><code>&lt;FrameLayout
                  android:id="@+id/FrameLayout3"
                  android:layout_width="40dp"
                  android:layout_height="40dp"
                  android:layout_marginLeft="25dp"
                  android:layout_marginTop="30dp"
                  android:layout_weight="1"&gt;
  
              &lt;com.twitter.sdk.android.core.identity.TwitterLoginButton
                  android:id="@+id/twitter1"
                  android:layout_width="40dp"
                  android:layout_height="40dp"
                  android:visibility="gone"/&gt;
  
              &lt;ImageButton
                  android:id="@+id/twitter_icon"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@drawable/twitter_icon" /&gt;
              &lt;/FrameLayout&gt;`
  </code></pre>
  
  <p>After clicking on the button, it is redirecting to the twitter login page and after clicking on "connect" button, it simply returns to the fragment and doesn't show the Toast message. When I Click the button for the second time, it shows toast from failure method.</p>