Architecture-samples: Why using SingleLiveEvent ?

Created on 31 Aug 2017  Â·  6Comments  Â·  Source: android/architecture-samples

IMO, it add complexity.

Snackbar message has just to be stored in the ViewModel. On configuration change, when the Activity/fragment is binded again the message is still in the ViewModel and the snackbar has just to be shown again.

Navigation could just be cut from the ViewModel behaviours to be directly executed from the View as there is no additional processing in the ViewModel.

Most helpful comment

Yes! Hopefully it will work without workarounds for multiple observers.

All 6 comments

There's already a thread about this, please don't open new ones.

You want to show the Snackbar message after every rotation? I don't think that's a good idea.

There's always processing in the ViewModel. For example if you want to save before exit.

Sorry, what is its name ?

As you answer here, I reply here :

Effectively, it seems to be a bad idea. SingleLiveEvent seems the best way to do that.

It's true but do we have to use it for each navigation action (eg for the addNewTask) ?

You don't have to use it :) but it's a good idea even if the ViewModel has no logic and the call comes from the activity. Keep the activities and fragments as dumb as possible.

I haven't found separate thread about SingleLiveEvent so posting here.

It's working like charm for me. Are there any plans to integrate it into library?

Yes! Hopefully it will work without workarounds for multiple observers.

Was this page helpful?
0 / 5 - 0 ratings