简体   繁体   English

在 firebase 中插入数据后,活动被破坏

[英]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 -登录后,您会在 ListView 中看到注册用户列表。这是相关的 onclick 代码 -

    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().如果您单击一个用户,它会转到 ChatActivity.java,其中当前用户名和您单击的名称列表视图项在 OnCreate() 中分别作为 currentuser 和 chatbuddy 接收。 Then I have and sendMesg onclick method which inserts data in firebase which are mainly the Chat and a time stamp-然后我有并 sendMesg onclick 方法在 firebase 中插入数据,主要是聊天和时间戳-

    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.我们登录后从列表视图中为每个新的 ChatBuddy 正确插入数据,我们 select 但每次单击 sendMesg,ChatActivity 消失,我 go 回到用户列表,这是我们登录后看到的第一页。 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(). Firebase eventListeners(ListenerForSingleValueEvent 除外)在您退出活动后保持运行,直到您使用 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.当您添加一条消息时,他们会尝试更改相关活动的 ui 并且您的应用程序崩溃。

This is how i solved my problem.Registration query for firebase was-这就是我解决问题的方法。firebase 的注册查询是-

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

After registration when user logged in and selected a chatBuddy from userList to chat with -注册后,用户登录并从 userList 中选择一个 chatBuddy 与之聊天 -

DatabaseReference for chat insertion was -聊天插入的 DatabaseReference 是 -

FirebaseDatabase.getInstance().getReference().child("users").child("chats"); 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.因此,注册用户最后保存的 state 受到一些干扰,因为聊天数据在同一个父节点下。

Only change i had to make for DatabaseReference for chat insertion was -我必须为 DatabaseReference 进行聊天插入的唯一更改是 -

FirebaseDatabase.getInstance().getReference().child("chats"); 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.也就是说,我从数据库引用中删除了.child("users"),因此聊天已成为所有其他 currentUser_chatBuddy 对的新节点或父节点。 Thus no interference in "users" parent node's last saved state.因此不会干扰“用户”父节点上次保存的 state。 Thus no more crash.因此不再崩溃。 Problem solved.问题解决了。 无问题的数据插入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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