简体   繁体   English

Firebase 如何在 Android 中打开其他活动时删除 ValueEventListeners

[英]Firebase how to ValueEventListeners remove when other activity open in Android

I'm editing the question for more clarity为了更清楚,我正在编辑问题

In my Activity A , I have have started my new Activity B for certain condition.在我的Activity A中,我已经在某些情况下开始了我的新Activity B OnCreate of Activity A has the following Activity A 的 OnCreate 有以下内容

final DatabaseReference reff = FirebaseDatabase.getInstance().getReference("Example").child(userid);
        reff.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {`
                    navHeaderName.setText(Objects.requireNonNull(dataSnapshot.child("name").getValue()).toString());
                    navHeaderId.setText(Objects.requireNonNull(dataSnapshot.child("member_ID").getValue()).toString());
                } else
                    reff.removeEventListener(this);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.d("RF-NavSet", databaseError.toString());
            }
        });

` `

In the fragment Af is also si inflated in Activity Afragment Af也在Activity A中膨胀

FragmentManager fm = getSupportFragmentManager();
        fragment = new FragmentOfActiityA(); 
fm.beginTransaction().replace(R.id.doc_fragment_container, fragment).commit();

In Fragment A also I have Firebase ValueEventListener在片段 A 中,我也有 Firebase ValueEventListener

listener = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                TextView text1 = view.findViewById(R.id.some_text1);
                TextView text2 = view.findViewById(R.id.some_text2);
                TextView text3 = view.findViewById(R.id.some_text3);

                text1.setText(Objects.requireNonNull(dataSnapshot.child(userId).child("name").getValue()).toString());
                text2.setText(Objects.requireNonNull(dataSnapshot.child(userId).child("specialisation").getValue()).toString());
                text3.setText(Objects.requireNonNull(dataSnapshot.child(userId).child("member_ID").getValue()).toString());
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.d("RF - SetProfile", databaseError.getMessage());
            }
        };

        reff.addListenerForSingleValueEvent(listener);

Here reff and listener are globally declared.这里refflistener是全局声明的。

When I destroy Activity A, I want to remove listener in Activity A and Fragment Af .当我销毁 Activity A 时,我想删除Activity AFragment Af中的侦听器。

How can I do that?我怎样才能做到这一点?

If I change any database value from some other Activity X , Activity A is automatically triggered due to these ValueEventListener which I dont want.如果我从其他一些Activity X更改任何数据库值,由于这些我不想要的 ValueEventListener 会自动触发Activity A

Remove the listener in your onDestroy() of activity A删除活动AonDestroy()中的侦听器

mDatabaseRef.child(key).removeListener(mListener);

And your activity A in background when open activity B and if you want to remove listener then remove listener in onPause() and add listener in onResume()当打开活动B时,您的活动A在后台,如果您想删除侦听器,则在onPause()中删除侦听器并在onResume()中添加侦听器

I have got the answer for the problem.我已经得到了问题的答案。 There one previous Activity O which I missed to removeEventListener which was secretly launching Activity A .有一个之前的Activity O我错过了 removeEventListener ,它正在秘密启动Activity A

Took a while to understand that花了一段时间才明白

暂无
暂无

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

相关问题 Android - Firebase 通知在点击时打开一个活动 - Android - Firebase Notification open an Activity when clicked 单击Firebase Notification时,打开MainActivity以外的其他活动 - Open a different activity other than MainActivity when clicking Firebase Notification 多个(嵌套)ValueEventListeners在Android的Firebase实时数据库中不起作用 - multiple(nested) ValueEventListeners not working in firebase realtime database for android 在Firebase RecyclerView Cardview中单击项目时如何打开详细信息活动 - How to open a details activity when an item is clicked in Firebase RecyclerView Cardview 单击通知时如何打开MainActivity以外的其他活动 - How to open another activity other than the MainActivity when clicking on notification 单击带有Firebase的通知时打开通知活动 - Open Notification activity when clicked on Notification with firebase 如何从 Android Studio 中的主活动以外的默认活动点击按钮打开链接? - How to Open a link on button click From a Default Activity other than Main Activity in Android Studio? 如何在片段中打开活动页面,当按下 android 中的通知时 - How to open activity page in fragment, when pressed a notification in android 在Android中重新启动我的应用程序时如何打开非主要活动 - How to open not main activity when restart my application in android 如何使用 firebase 数据库在 recyclerview 中打开新活动 - How to open new activity in recyclerview with firebase database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM