Pavel
Pavel

Reputation: 5876

How to add an Fragment-scoped listener for Task?

Task used in Firebase has method addOnSuccessListener(Activity, OnSuccessListener). The listener added using this method is automatically removed during activity's onStop().

What is the nearest equivalent for fragment? I.e. I want to add a listener inside a fragment but don't keep it after the fragment is closed.

Upvotes: 2

Views: 294

Answers (1)

h0102
h0102

Reputation: 256

You can use LiveData & ViewModel:

  1. create ViewModel & "Success Event" LiveData in ViewModel
  2. observer that liveData from your fragment
  3. when receiving OnSuccessListener() -> update the above LiveData content

Upvotes: 1

Related Questions