简体   繁体   中英

what happen if ValueEventListener not remove from activity onDestroy firebase realtime database

what happens when ValueEventListener is not removed when activity is destroyed. Does this event listener listen even after activity destroys?

myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
                \\todo
        }});

Firebase Realtime Database listeners are not automatically scoped to the context/activity. So the listener will indeed remain active until you explicitly remove it, or until the application exits.

If the code in your onDataChange accesses the UI elements of the activity, this may lead to unexpected results and crashes. For this reason it is common to remove the listener in a lifecycle event, such as onStop or onPause .

If you don't remove the listener, then it will keep on listening. You will effectively "leak" the listener if you don't remove it when it's no longer needed. The listener knows nothing about the Android activity lifecycle, so you have will have to add code to remove it at the right time.

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