简体   繁体   中英

activity gets destroyed after inserting data in firebase

So I am creating a Chat app which allows registration first. And after you login you see a list of registered users in ListView.This is the relevant onclick code -

    usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            String currentUser = getIntent().getExtras().getString("currUser");
            String chatBuddy = adapter.getItem(position);
            Intent goChatActivity = new Intent(getApplicationContext(), ChatActivity.class);

            goChatActivity.putExtra("currentUser", currentUser);
            goChatActivity.putExtra("chatBuddy", chatBuddy);
            startActivity(goChatActivity);
        }
    });

If you click on a user it takes to ChatActivity.java where both the current username and the name listview item you clicked are received as currentuser and chatbuddy respectively in OnCreate(). Then I have and sendMesg onclick method which inserts data in firebase which are mainly the Chat and a time stamp-

    sendMesg.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

    databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {


            final Calendar cal = Calendar.getInstance();

            Chat chat = new Chat(chatEditText.getText().toString(), cal.getTime().toString());
            databaseReference.child(currentUser+"/"+currentUser+"_"+ chatBuddy).push().setValue(chat);


        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

The data gets inserted properly for every new ChatBuddy we select from listview after we login but everytime in click on sendMesg the ChatActivity disappears and i go back to UsersList which is the first page we see after we login. Here is the link of demo chat screen record

Firebase eventListeners(except ListenerForSingleValueEvent) stay running after you exit the activity, until you remove them by using removeListener().

if you use any listeners for messages in other activities, they may be still running. And when you add a message they try to change related acivity's ui and your app crashes.

This is how i solved my problem.Registration query for firebase was-

FirebaseDatabase.getInstance().getReference().child("users");

After registration when user logged in and selected a chatBuddy from userList to chat with -

DatabaseReference for chat insertion was -

FirebaseDatabase.getInstance().getReference().child("users").child("chats");

And the chat would appear as such - 有问题的数据插入

That meant chats were being saved as child of parent node users. Hence some interference in the last saved state of Registered users as chat data was going under same parent node.

Only change i had to make for DatabaseReference for chat insertion was -

FirebaseDatabase.getInstance().getReference().child("chats");

That is i removed.child("users") from database reference so chats has become a new node or parent node for all other currentUser_chatBuddy pairs. Thus no interference in "users" parent node's last saved state. Thus no more crash. Problem solved. 无问题的数据插入

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