question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

View actions (snackbar, activity navigation, ...) in ViewModel

See original GitHub issue

Sometimes the ViewModel needs to invoke an action using an Activity (for example to show a snack bar or to navigate to a new activity). Using MVP it’s easy to implement it because the presenter has a reference to the Activity. The ViewModel doesn’t contain a reference to the Activity, what’s the best way to implement this feature? In the GithubBrowserSample you created a method getErrorMessageIfNotHandled to solve this problem, are there other solutions? In a demo project I am working on I have created a UiActionsLiveData class that allows the ViewModel to invoke an action on the view. The connection between the ViewModel and the view is managed using a LiveData so the ViewModel doesn’t contain a reference to the View even if it can invoke actions on it. The usage is simple, the ViewModel can add an action to the UiActionsLiveData:

uiActions.execute { navigationController.showError(it, t.message) }

Thanks to the LiveData implementation the action will be executed on the Activity when it’s resumed. I like this solution but it’s a bit complicated, an official solution would be great.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:93
  • Comments:84 (13 by maintainers)

github_iconTop GitHub Comments

58reactions
sockeqwecommented, Jun 22, 2017

A big NO from my side for SingleLiveEvent! Unidirectional Dataflow and immutability ftw! (also a little state reducer over here and there doesn’t hurt)

SearchViewModel which offers a LiveData<LoadMoreState> getLoadMoreStatus() method for the view to subscribe / listen to. So once the view has displayed the error message (i.e. with a snackbar) the View calls:

viewModel.setLoadMoreErrorShown();

This will then force the viewModel to emit a new (immutable) LoadMoreState.

class SearchViewModel extends ViewModel {
   private MutableLiveDate<LoadMoreState> loadMoreState;
   ...
   
   // Subscribed by the View
   public LiveData<LoadModeState> getLoadMoreStatus() {
       return loadMoreState;
    }

   public void setLoadMoreErrorShow(){
         loadMoreState.setValue(new LoadMoreState(false, null)); // null means no error message to display , false means don't display loading more progress bar
    }
}

behaves like SingleLiveEvent (i.e. after screen orientation change, snackbar wont show again, because LoadMoreState.errorMessage == null) but is immutable!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android: MVVM is it possible to display a message (toast ...
Display Toast/snackbar message in view (Activity/Fragment) from viewmodel using LiveData. Step: Add LiveData into your viewmodel; View just ...
Read more >
Using navigation component in ViewModels on Android ...
In this article, I will be talking about navigation. Nowadays in Android development, the best practice is to use single activity and ...
Read more >
Android Snackbar Example Tutorial - DigitalOcean
The code snippet for Action Call Snackbar button is given below: two.setOnClickListener(new View.OnClickListener() { @Override public void ...
Read more >
How to Add Action Snackbar in Android? - GeeksforGeeks
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for...
Read more >
Sending ViewModel Events to the UI in Android - Droidcon
LiveData with SnackBar, Navigation and other events (the SingleLiveEvent ... actions between an Android view model and its associated view.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found