I currently use LifecycleObserver
for notifying about events such as onStart
of my view. This is great for components as presenters or handlers, such as FacebookLoginHandler
that needs to register the callback once the view is ready. However, there are some situations, as the one I've mention, that I wanted my handler to handle the returned data of another activity.
Example:
When I choose to login with Facebook, it starts another activity and the return of it goes to view's onActivityResult
method. this makes me inject my FacebookLoginHandler
in the view, only to delegate the handling back to it. I wanted to use LifecycleObserver
to get notified of onActivityResult
and avoid having to couple my handler to the view only for delegating this event. Is there a way to do that?
Step 1: Define LiveData object to store data and a method to observe that data in View class. Step 3: Define viewModel object in activity. this refers to an instance of lifecycleOwner . We are observing Data changes in activity so activity will be notified when live data has been set.
Lifecycle Awareness: ViewModel objects are also lifecycle-aware. They are automatically cleared when the Lifecycle they are observing gets permanently destroyed.
The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.
I had exactly the same problem. I have "sign in with Facebook" functionality in my app and I want to move this functionality into separate class AuthManager
that implements LifecycleObserver
.
I have checked Lifecycle.Event
class that contains all the available lifecycle events. Looks like at the moment (android.arch version 1.1.0) there is no event for hading onActivityResult()
with LifecycleObserver
.
Opened an issue about this: https://github.com/googlesamples/android-architecture-components/issues/317
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With