title: Android - why does it return to the first activity
body: <p>I'm developing an chat app using Firebase as database.
  In the app i have 3 activitys: LoginActivity,MainActivity and chatActivity.
  In the MainActivity i can open a chat room and then enter to the chat room.
  Then it opens the chatActivity.
  On the chat activity the users can send message to each other. And here my problem is. When the user type a message and press send. for some reason when the user press the SEND button it jumps to the mainActivity instand send the message. i have no idea whats going on and i need your help guys.</p>
  
  <p><strong>Main activity code</strong></p>
  
  <pre><code>    package com.world.bolandian.firebase;
  
  
   public class MainActivity extends AppCompatActivity {
  
  private Button createChat;
  private EditText chatRoomName;
  private ListView listView;
  private ArrayAdapter&lt;String&gt; arrayAdapter;
  private ArrayList&lt;String&gt; listOfChats = new ArrayList&lt;&gt;();
  private String name;
  private DatabaseReference root  = FirebaseDatabase.getInstance().getReference().getRoot();
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
         ifUserIsLoggedIn();
  
      createChat = (Button)findViewById(R.id.createChatBtn);
      chatRoomName = (EditText)findViewById(R.id.chatRoomEt);
      listView = (ListView)findViewById(R.id.listView);
  
      arrayAdapter = new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_dropdown_item_1line, listOfChats);
  
      listView.setAdapter(arrayAdapter);
  
      enterUserName();
  
      createChat.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
  
              Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
              map.put(chatRoomName.getText().toString(), "");
              root.updateChildren(map);
          }
      });
  
      root.addValueEventListener(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
  
              Set&lt;String&gt; set = new HashSet&lt;String&gt;();
              Iterator i = dataSnapshot.getChildren().iterator();
  
              while (i.hasNext()) {
                  set.add(((DataSnapshot) i.next()).getKey());
              }
              listOfChats.clear();
              listOfChats.addAll(set);
              arrayAdapter.notifyDataSetChanged();
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
  
          }
      });
  
      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  
              Intent intent = new Intent(getApplicationContext(),ChatRoom.class);
              intent.putExtra("roomName",((TextView)view).getText().toString());
              intent.putExtra("userName",name);
              startActivity(intent);
          }
      });
  
  
        }
  
  
  private void ifUserIsLoggedIn(){
  
      FirebaseAuth auth = FirebaseAuth.getInstance();
      FirebaseUser user = auth.getCurrentUser();
      if(user != null){
  
      }
      else
          startActivity(new Intent(this,LoginActivity.class));
  }
  
  
      private void enterUserName(){
  
          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setTitle("Please enter A nick name:");
  
         final EditText input = new EditText(this);
  
          builder.setView(input);
          builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  name = input.getText().toString();
              }
          });
  
          builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  dialog.cancel();
                  enterUserName();
              }
          });
          builder.show();
       }
  
     }
  </code></pre>
  
  <p><strong>This is ChatActivity</strong></p>
  
  <pre><code>  public class ChatRoom extends AppCompatActivity {
  
  private Button sendMessageBtn;
  private EditText inputMsg;
  private TextView showMessage;
  private DatabaseReference root;
  private String userName,chatName,tempKey;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_chat_room);
  
  
      sendMessageBtn = (Button)findViewById(R.id.sendmessagebtn);
      inputMsg = (EditText)findViewById(R.id.messageEt);
      showMessage = (TextView)findViewById(R.id.showMessage);
  
      userName = getIntent().getExtras().get("userName").toString();
      chatName = getIntent().getExtras().get("roomName").toString();
      Log.v("username",userName);
      Log.v("chatname",chatName);
  
      setTitle(" Room " + chatName);
  
      root = FirebaseDatabase.getInstance().getReference().child(chatName);
  
      sendMessageBtn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Map&lt;String,Object&gt; map = new HashMap&lt;String, Object&gt;();
              tempKey = root.push().getKey();
              root.updateChildren(map);
  
              DatabaseReference messageRoot = root.child(tempKey);
              Map&lt;String,Object&gt; map2 = new HashMap&lt;String, Object&gt;();
              map2.put("name",userName);
              map2.put("message",inputMsg);
  
              messageRoot.updateChildren(map2);
          }
      });
  
  
      root.addChildEventListener(new ChildEventListener() {
          @Override
          public void onChildAdded(DataSnapshot dataSnapshot, String s) {
  
              chatConversation(dataSnapshot);
          }
  
          @Override
          public void onChildChanged(DataSnapshot dataSnapshot, String s) {
  
              chatConversation(dataSnapshot);
          }
  
          @Override
          public void onChildRemoved(DataSnapshot dataSnapshot) {
  
          }
  
          @Override
          public void onChildMoved(DataSnapshot dataSnapshot, String s) {
  
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
  
          }
      });
  
  }
  
  private String chatMsg,chatUserName;
  
  private void chatConversation(DataSnapshot dataSnapshot){
  
      Iterator i = dataSnapshot.getChildren().iterator();
          while(i.hasNext()){
              chatMsg = (String)((DataSnapshot)i.next()).getValue();
              chatUserName = (String)((DataSnapshot)i.next()).getValue();
  
              showMessage.append(chatUserName + " : " + chatMsg +"\n" );
          }
      }
   }
  </code></pre>
  
  <hr>
  
  <p>Thats the code guys</p>