简体   繁体   中英

previous activity's method being called again onBackPress

Screenshot 1 - https://ibb.co/k5O8Eo

Screenshot 2 - https://ibb.co/bz5MZo

Screenshot one is how its supposed to behave and it does behave like that when I open it... but after I open the activity and go to the next activity and come back... the same messages are being fetched again and showing twice although only in the database its only once... how do I solve this problem?

    @Override
protected void onStart() {
    super.onStart();
    fetchMessages();
}

private void fetchMessages() {
   mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId).addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Messages message = dataSnapshot.getValue(Messages.class);
            messagesList.add(message);
            mAdapter.notifyDataSetChanged();
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
        }
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
        }
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            throw databaseError.toException();
        }
    });
}

This is happening because you are calling fetchMessages() method from inside the onStart() method, which means that each you stop and then start the activity, this method is triggered and you are ending up in having duplicate data.

To solve this, move the call of the method in the onCreate() method so you'll have the data displayed only once even if you are returning from another activity or if the onBackPress() method is called.

It is probably happening because your activity is in backstack and every time you come back the onStart method is called which results in multiple fetches.

You can clear the list in onStop()

 @Override
 public void onStop(){
 if(messagesList != null){
   messageList.clear();
}
}

You call fetchMessages() every time your activity starts, and in that you add a listener to Firebase. This means that the second time the activity is started, you end up adding a second listener. So at that point you'll receive the data from Firebase twice.

The solution is to remove the listener in the corresponding lifecycle method. Since you add the listener in onStart , you should remove it in onStop :

public void onStop() {
  super.onStop();
   mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId).removeChildEvenListener(mListener);
}

You'll note that we need a mListener in here, so you'll need to capture that in onStart :

mListener = mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId).addChildEventListener(new ChildEventListener() {
  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM