Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use Android's LifecycleObserver to notify about onActivityResult?

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?

like image 394
Humble Student Avatar asked Jan 06 '18 10:01

Humble Student


People also ask

How can you make a class lifecycle-aware in Android implement lifecycleOwner?

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.

Is ViewModel Life Cycle Aware?

Lifecycle Awareness: ViewModel objects are also lifecycle-aware. They are automatically cleared when the Lifecycle they are observing gets permanently destroyed.

How do I use onActivityResult?

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.


1 Answers

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

like image 97
Vadims Savjolovs Avatar answered Sep 18 '22 11:09

Vadims Savjolovs