Netanel Malichi
Netanel Malichi

Reputation: 33

My Activity Launches Twice

I have listview with setOnItemClickListener() method, when clicked I want it to open new activity and give it 2 extras, the number of the item and the user that logged in (back in home screen), the problem is that the activity starts twice, one with good extras and one with "0" in the position extra...

this code is supposed to check if Firebase server has place in it (no more than 5 players) and if so add the user to the server and join the room (launch ServerActivity).

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
            myRef = database.getReference("Servers/S"+position);
            myRef.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    int i = 0;
                    while (dataSnapshot.child("player"+i).child("username").getValue() != null)
                        i++;
                    if (i < 5) {
                        Log.d("Implementing Child","player"+i);
                        CustomUser customUser = new CustomUser(false,currentUser.getUsername(),"",0,currentUser.getWins());
                        myRef.child("player" + i).setValue(customUser);

                    Intent intent = new Intent(GameActivity.this,ServerActivity.class);
                    intent.putExtra("serverNum",position);
                    intent.putExtra("currentUser",currentUser);
                    Log.d("position", position + "");
                    startActivity(intent);
                    finish();
                    }
                    else{
                        Toast.makeText(getApplicationContext(), "Server Full", Toast.LENGTH_SHORT).show();
                        myRef.child("isJoinable").setValue(false);
                    }
                    adapter.notifyDataSetChanged();


                }
                @Override
                public void onCancelled(DatabaseError error) {
                    // Failed to read value
                    Toast.makeText(getApplicationContext(), "" +
                            "Internet Error", Toast.LENGTH_SHORT).show();
                }
            });
            Intent intent = new Intent(GameActivity.this,ServerActivity.class);
            intent.putExtra("currentUser",currentUser);
            startActivity(intent);
            finish();
            //TODO FIX: activity probebly launches twice
        }
    });

I tried modifing LaunchMode in the manifest but it resulted with fail causing the "serverNum" extra to be 0 instead of the position.

Upvotes: 1

Views: 586

Answers (1)

Ankur Aggarwal
Ankur Aggarwal

Reputation: 2220

Why have you added this code :

        Intent intent = new Intent(GameActivity.this,ServerActivity.class);
        intent.putExtra("currentUser",currentUser);
        startActivity(intent);
        finish();

This would cause your ServerActivity to launch as soon as the listener is added. Have you tried commenting this code and use only the code in onDataChange?

Upvotes: 2

Related Questions