在onActivityCreated中被调用了三次。

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

onActivityCreated launched three times

问题

  1. //监听游戏开始事件:
  2. mStart = FirebaseDatabase.getInstance().getReference().child("Rooms").child(roomId).child("Proprieties").child("Status");
  3. mStart.addValueEventListener(new ValueEventListener(){
  4. @Override
  5. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  6. if (dataSnapshot.getValue().equals("GAME")){
  7. Intent intent = new Intent(WaitGameActivity.this, GameActivity.class);
  8. startActivity(intent);
  9. finish();
  10. }
  11. }
  12. @Override
  13. public void onCancelled(@NonNull DatabaseError databaseError) { }
  14. });

日志:

  1. V/FA: onActivityCreated
  2. V/FA: onActivityCreated
  3. 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.

  1. //LISTEN IF THE GAME START ::
  2. mStart = FirebaseDatabase.getInstance().getReference().child("Rooms").child(roomId).child("Proprieties").child("Status");
  3. mStart.addValueEventListener(new ValueEventListener(){
  4. @Override
  5. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  6. if (dataSnapshot.getValue().equals("GAME")){
  7. Intent intent = new Intent(WaitGameActivity.this,GameActivity.class);
  8. startActivity(intent);
  9. finish();
  10. }
  11. }
  12. @Override
  13. public void onCancelled(@NonNull DatabaseError databaseError) { }
  14. });

And Log :

  1. V/FA: onActivityCreated
  2. V/FA: onActivityCreated
  3. V/FA: onActivityCreated

答案1

得分: 1

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

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

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

  1. mStart.addValueEventListener(new ValueEventListener(){
  2. @Override
  3. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  4. mStart.removeValueEventListener(this); //remove once you get the event
  5. if (dataSnapshot.getValue().equals("GAME")){
  6. Intent intent = new Intent(WaitGameActivity.this,GameActivity.class);
  7. startActivity(intent);
  8. finish();
  9. }
  10. }
  11. @Override
  12. public void onCancelled(@NonNull DatabaseError databaseError) { }
  13. });

答案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:

确定