Hello.

I recently discovered that ValueEventListener , tied to Firebase , continued to work even after calling StartActivity in it and then finish() . It became noticeable, because inside there was a call for a new Activity and after each change in the database, that method was called.

It turns out that all Listener'ы that I created in Activity work all the time until the program process is completely turned off, but they do not manifest themselves explicitly, since just update the ArrayList'ы in the background. In this regard, a reasonable question arose: is it necessary to completely kill an Activity when calling a new one? On enSO I read that it is enough to call return after finish() , but this is not always possible. In my case, I simply called the removeEventListener() method removeEventListener() DatabaseReference object after calling the new Activity .

Based on these reflections, I came to the conclusion that you need to delete the EventListener every time you switch to a new Activity . But how does the transition to the previous Activity by pressing the "back" button? Will the onCreate method be called again in that Activity ? If not, then the Listener will not be reinstalled.

In short, help resolve my dilemma. Thank you in advance for your response.

  • Try podding in onResume and unsubscribing onPause - YuriiSPb
  • @Yuriy SPb I understand that onPause() is called when a new Activity called over the current one. Is it called when finish() called? Or do you need to separately register onStop() ? And do I need to transfer the initialization of components from onCreate to onResume or only to reinstall the Listener in onResume ? - ahgpoug
  • When the activation is completed, onPause will be called. onStop may not be needed. The transfer of initialization is a difficult question. I think you can try to limit the installation of listeners in onResume - YuriySPb
  • @ YuriySPb thanks for the advice, implemented - it seems to work. I think you can post it as an answer. - ahgpoug

1 answer 1

In general, you can subscribe to events in onResume and unsubscribe at onPause - onPause will be called when you exit from activation (no matter what kind of departure - switching to the next activation, returning to the previous one, minimizing the application), onResume - if activated.

But you need to be careful - onPause will be called including. when opening conversations.

  • And you can still clarify about the last expression? After all, BottomSheetDialogFragment also apply to dialog? And is the onResume method onResume after it is closed? UPD: now checked: when the BottomSheetDialogFragment open BottomSheetDialogFragment update still goes. - ahgpoug
  • @ahgpoug you can log some of these methods to the logs to understand when they are being called. In theory, the display of such a dialogue should call onPause. And after it is hidden, it should be called onResume. If this does not happen - perhaps this is a feature of the implementation of this type of dialogue. - YurySPb
  • I just checked it. Thanks for the answer. - ahgpoug