移动到另一个页面时停止收听

时间:2017-09-07 23:26:46

标签: android firebase firebase-realtime-database

我希望在收听停止时离开页面。

FirebaseDatabase firebaseDatabase;
DatabaseReference myRef;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_showcomments);


}




    private void callFireBase()
        {
            firebaseDatabase = FirebaseDatabase.getInstance();
            myRef = firebaseDatabase.getReference();
            myRef.child(id).addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                    Toast.makeText(showcomments.this, "" + dataSnapshot.child("comment").getValue().toString(), Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {

                }

                @Override
                public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Toast.makeText(showcomments.this, "hey", Toast.LENGTH_SHORT).show();
                }
            });
        }


        @Override
        protected void onPause() {
            super.onPause();
            myRef.removeEventListener(firebaseDatabase);
        }

当我转到另一个页面时,我从应用程序中退出,我想停止收听,我尝试了很多并在同一网站上搜索主题,但没有发现任何有用的

1 个答案:

答案 0 :(得分:1)

ChildEventListener中实施Activity,然后在离开Activity时将其删除。

public class MyActivity extends Activity implements ChildEventListener {

    FirebaseDatabase firebaseDatabase;
    DatabaseReference myRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_showcomments);
    }

    private void callFireBase() {
        firebaseDatabase = FirebaseDatabase.getInstance();
        myRef = firebaseDatabase.getReference();
        myRef = myRef.child(id);
        myRef.addChildEventListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        myRef.removeEventListener(this);
    }

    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {
        Toast.makeText(showcomments.this, "" + dataSnapshot.child("comment").getValue().toString(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Toast.makeText(showcomments.this, "hey",
                Toast.LENGTH_SHORT).show();
    }


}

请执行此操作并相应地保存DatabaseReference您想要的removeListener