【问题标题】:My Activity Launches Twice我的活动启动两次
【发布时间】:2017-05-30 15:21:28
【问题描述】:

我有 setOnItemClickListener() 方法的列表视图,当点击时我希望它打开新活动并给它 2 个额外内容,项目编号和登录用户(返回主屏幕),问题是活动开始两次,一次具有良好的附加功能,另一种具有“0”位置的附加功能...

此代码应该检查 Firebase 服务器是否在其中(不超过 5 个玩家),如果有,则将用户添加到服务器并加入房间(启动 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
        }
    });

我尝试在清单中修改 LaunchMode,但结果失败,导致“serverNum”额外为 0 而不是位置。

【问题讨论】:

    标签: android android-studio android-activity firebase firebase-realtime-database


    【解决方案1】:

    为什么要添加此代码:

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

    这将导致您的ServerActivity 在添加侦听器后立即启动。您是否尝试过注释此代码并仅使用 onDataChange 中的代码?

    【讨论】:

    • 那行得通,试探性地复制了这段代码并忘记从那里删除它,已经找了好几个小时了,我现在感觉有点愚蠢,但是谢谢!
    • @NetanelMalichi 这正是 Android Studio 调试器非常有用的情况。我强烈建议您学习如何使用它。
    猜你喜欢
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 2020-06-10
    相关资源
    最近更新 更多