title: Android: Unable to navigate from one fragment to another
body: <p><a href="https://i.stack.imgur.com/n5kEQ.png" rel="nofollow noreferrer">This is the login screen, which leads to the next screen(Chat Screen)</a></p>
  
  <p><a href="https://i.stack.imgur.com/i2LIy.png" rel="nofollow noreferrer">This is the Chat Screen- which I am unable to navigate to</a></p>
  
  <p>I am trying to implement a chat feature wherein both the login and the chat screens are fragments.I am not any getting errors. But, the app is not navigating to the chat screen. I don't know where I am missing.
  Any kind of suggestions would be appreciated.</p>
  
  <p>******** Login Fragment********</p>
  
  <pre><code>public class LoginFragment extends Fragment {
  
  private EditText mUsernameView;
  private Button signInButton;
  private String mUsername;
  private Socket mSocket;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
      View loginview = inflater.inflate(R.layout.fragment_login, container, false);
  
      ChatApplication app = (ChatApplication) getActivity().getApplication();
      mSocket = app.getSocket();
  
      // this is the login form
      mUsernameView = (EditText) loginview.findViewById(R.id.username_input);
      mUsernameView.setOnEditorActionListener(new TextView.OnEditorActionListener()
      {
          @Override
          public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent)
          {
              if(id == R.id.login || id == EditorInfo.IME_NULL)
              {
                  attemptLogin();
                  return true;
              }
                  return false;
          }
      });
  
      signInButton = (Button) loginview.findViewById(R.id.sign_in_button);
      signInButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v)
          {
              attemptLogin();
          }
      });
  
      mSocket.on("login",onLogin);
  
      return loginview;
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
  
      mSocket.off("login", onLogin);
  }
  
  /*
      This method attempts the following
      1- clears the error message/resets the error message.
      2- checks if the user has entered valid username
      3- attempts user login
   */
  private void attemptLogin()
  {
      mUsernameView.setError(null);  //1
      String username = mUsernameView.getText().toString().trim();
      if(TextUtils.isEmpty(username))  //2
      {
          mUsernameView.setError("This field is required!");
          mUsernameView.requestFocus();
          return;
      }
  
      mUsername = username;
  
      mSocket.emit("add user", username); //3
  }
  
  private Emitter.Listener onLogin = new Emitter.Listener()
  {
      @Override
      public void call(Object... args)
      {
          JSONObject data = (JSONObject) args[0];
          int numUsers;
          try
          {
              numUsers = data.getInt("numUsers");
          }
          catch (JSONException e)
          {
              return;
          }
  
          Intent intent = new Intent();
          intent.putExtra("username",mUsername);
          intent.putExtra("numUsers",numUsers);
          getActivity().setResult(RESULT_OK,intent);
          getActivity().finish();
      }
  };
  </code></pre>
  
  <p>}</p>
  
  <p>************** Chat Fragment *************</p>
  
  <pre><code>   public class ChatFragment extends Fragment
  {
  
  
      private static final int REQUEST_LOGIN = 1;
  
      private static final int TYPING_TIMER_LENGTH = 600;
  
      private RecyclerView mMessagesView;
      private EditText mInputMessageView;
      private List&lt;Message&gt; mMessages = new ArrayList&lt;Message&gt;();
      private RecyclerView.Adapter mAdapter;
      private boolean mTyping = false;
      private Handler mTypingHandler = new Handler();
      private String mUsername;
      private Socket mSocket;
  
      private Boolean isConnected = true;
  
      public ChatFragment() {
          super();
      }
  
      @Override
      public void onAttach(Context context) {
          super.onAttach(context);
          mAdapter = new MessageAdapter(context, mMessages);
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setHasOptionsMenu(true);
  
          ChatApplication app = (ChatApplication) getActivity().getApplication();
          mSocket = app.getSocket();
          mSocket.on(Socket.EVENT_CONNECT,onConnect);
          mSocket.on(Socket.EVENT_DISCONNECT,onDisconnect);
          mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
          mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
          mSocket.on("new message", onNewMessage);
          mSocket.on("user joined", onUserJoined);
          mSocket.on("user left", onUserLeft);
          mSocket.on("typing", onTyping);
          mSocket.on("stop typing", onStopTyping);
          mSocket.connect();
  
          startSignIn();
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          return inflater.inflate(R.layout.fragment_chat, container, false);
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
  
          mSocket.disconnect();
  
          mSocket.off(Socket.EVENT_CONNECT, onConnect);
          mSocket.off(Socket.EVENT_DISCONNECT, onDisconnect);
          mSocket.off(Socket.EVENT_CONNECT_ERROR, onConnectError);
          mSocket.off(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
          mSocket.off("new message", onNewMessage);
          mSocket.off("user joined", onUserJoined);
          mSocket.off("user left", onUserLeft);
          mSocket.off("typing", onTyping);
          mSocket.off("stop typing", onStopTyping);
      }
  
      @Override
      public void onViewCreated(View view, Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
  
          mMessagesView = (RecyclerView) view.findViewById(R.id.messages);
          mMessagesView.setLayoutManager(new LinearLayoutManager(getActivity()));
          mMessagesView.setAdapter(mAdapter);
  
          mInputMessageView = (EditText) view.findViewById(R.id.message_input);
          mInputMessageView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
              @Override
              public boolean onEditorAction(TextView v, int id, KeyEvent event) {
                  if (id == R.id.send || id == EditorInfo.IME_NULL) {
                      attemptSend();
                      return true;
                  }
                  return false;
              }
          });
          mInputMessageView.addTextChangedListener(new TextWatcher() {
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              }
  
              @Override
              public void onTextChanged(CharSequence s, int start, int before, int count) {
                  if (null == mUsername) return;
                  if (!mSocket.connected()) return;
  
                  if (!mTyping) {
                      mTyping = true;
                      mSocket.emit("typing");
                  }
  
                  mTypingHandler.removeCallbacks(onTypingTimeout);
                  mTypingHandler.postDelayed(onTypingTimeout, TYPING_TIMER_LENGTH);
              }
  
              @Override
              public void afterTextChanged(Editable s) {
              }
          });
  
          ImageButton sendButton = (ImageButton) view.findViewById(R.id.send_button);
          sendButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  attemptSend();
              }
          });
      }
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if (Activity.RESULT_OK != resultCode) {
              getActivity().finish();
              return;
          }
  
          mUsername = data.getStringExtra("username");
          int numUsers = data.getIntExtra("numUsers", 1);
  
          addLog(getResources().getString(R.string.message_welcome));
          addParticipantsLog(numUsers);
      }
  
      @Override
      public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
          // Inflate the menu; this adds items to the action bar if it is present.
          inflater.inflate(R.menu.menu_main, menu);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_leave) {
              leave();
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
      private void addLog(String message) {
          mMessages.add(new Message.Builder(Message.TYPE_LOG)
                  .message(message).build());
          mAdapter.notifyItemInserted(mMessages.size() - 1);
          scrollToBottom();
      }
  
      private void addParticipantsLog(int numUsers) {
          addLog(getResources().getQuantityString(R.plurals.message_participants, numUsers, numUsers));
      }
  
      private void addMessage(String username, String message) {
          mMessages.add(new Message.Builder(Message.TYPE_MESSAGE)
                  .username(username).message(message).build());
          mAdapter.notifyItemInserted(mMessages.size() - 1);
          scrollToBottom();
      }
  
      private void addTyping(String username) {
          mMessages.add(new Message.Builder(Message.TYPE_ACTION)
                  .username(username).build());
          mAdapter.notifyItemInserted(mMessages.size() - 1);
          scrollToBottom();
      }
  
      private void removeTyping(String username) {
          for (int i = mMessages.size() - 1; i &gt;= 0; i--) {
              Message message = mMessages.get(i);
              if (message.getType() == Message.TYPE_ACTION &amp;&amp; message.getUsername().equals(username)) {
                  mMessages.remove(i);
                  mAdapter.notifyItemRemoved(i);
              }
          }
      }
  
      private void attemptSend() {
          if (null == mUsername) return;
          if (!mSocket.connected()) return;
  
          mTyping = false;
  
          String message = mInputMessageView.getText().toString().trim();
          if (TextUtils.isEmpty(message)) {
              mInputMessageView.requestFocus();
              return;
          }
  
          mInputMessageView.setText("");
          addMessage(mUsername, message);
  
          // perform the sending message attempt.
          mSocket.emit("new message", message);
      }
  
      private void startSignIn() {
          mUsername = null;
      /* Error:
          Intent intent = new Intent(getActivity(), LoginFragment.class);
          startActivityForResult(intent, REQUEST_LOGIN);
      */
  
       // Solution 1: for calling a fragment from a fragment
          Intent intent = new Intent(getActivity(),LoginFragment.class);
          getActivity().startActivityForResult(intent,REQUEST_LOGIN);
  
  /*
       // Solution 2: for calling a fragment from a fragment
          LoginFragment loginFragment = new LoginFragment();
          FragmentManager fManager = getFragmentManager();
          FragmentTransaction fTransaction = fManager.beginTransaction();
          fTransaction.replace(R.id.id_fragment_chat,loginFragment);
          fTransaction.addToBackStack(null);
          fTransaction.commit();
  
  */
      }
  
      private void leave() {
          mUsername = null;
          mSocket.disconnect();
          mSocket.connect();
          startSignIn();
      }
  
      private void scrollToBottom() {
          mMessagesView.scrollToPosition(mAdapter.getItemCount() - 1);
      }
  
      private Emitter.Listener onConnect = new Emitter.Listener() {
          @Override
          public void call(Object... args) {
              getActivity().runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      if(!isConnected) {
                          if(null!=mUsername)
                              mSocket.emit("add user", mUsername);
                          Toast.makeText(getActivity().getApplicationContext(),
                                  R.string.connect, Toast.LENGTH_LONG).show();
                          isConnected = true;
                      }
                  }
              });
          }
      };
  .
  .
  .
  }
  </code></pre>