在onActivityCreated中被调用了三次。

huangapple go评论63阅读模式
英文:

onActivityCreated launched three times

问题

//监听游戏开始事件:
mStart = FirebaseDatabase.getInstance().getReference().child("Rooms").child(roomId).child("Proprieties").child("Status");
mStart.addValueEventListener(new ValueEventListener(){
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        if (dataSnapshot.getValue().equals("GAME")){
            Intent intent = new Intent(WaitGameActivity.this, GameActivity.class);
            startActivity(intent);
            finish();
        }
    }
    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) { }
});

日志:

V/FA: onActivityCreated
V/FA: onActivityCreated
V/FA: onActivityCreated
英文:

I want to start new activity when a value ("Status") change in my Database Firebase.
But i have problem because onActivityCreated is launched three times (because my onDataChange of my Listener is called several times). How can i fix this ? Thank you in advance.

//LISTEN IF THE GAME START ::
        mStart = FirebaseDatabase.getInstance().getReference().child("Rooms").child(roomId).child("Proprieties").child("Status");
        mStart.addValueEventListener(new ValueEventListener(){
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.getValue().equals("GAME")){
                    Intent intent = new Intent(WaitGameActivity.this,GameActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) { }
        });

And Log :

V/FA: onActivityCreated
V/FA: onActivityCreated
V/FA: onActivityCreated

答案1

得分: 1

你可以在接收到如下事件后移除值监听器:

mStart.addValueEventListener(new ValueEventListener(){
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) { 
        mStart.removeValueEventListener(this); // 在获得事件后移除监听器
        if (dataSnapshot.getValue().equals("GAME")){
            Intent intent = new Intent(WaitGameActivity.this,GameActivity.class);
            startActivity(intent);
            finish();
        }
    }
    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) { }
});
英文:

You could remove the value listener once you receive an event like below:

 mStart.addValueEventListener(new ValueEventListener(){
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) { 
                mStart.removeValueEventListener(this); //remove once you get the event
                if (dataSnapshot.getValue().equals("GAME")){
                    Intent intent = new Intent(WaitGameActivity.this,GameActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) { }
        });

答案2

得分: 1

onDataChange被调用多次,如果您没有正确移除监听器并且在每次打开活动时都在活动中创建了新的监听器实例。
在收到数据后调用此方法:
mStart.removeValueEventListener(this)

英文:

onDataChange is called multiple times in case you have not removed the listener properly and are creating a new instance of your listener in your activity every time you open it.

Call this once you recieved data:
mStart.removeValueEventListener(this)

huangapple
  • 本文由 发表于 2020年4月10日 19:26:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/61139314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定