Skip to content

Instantly share code, notes, and snippets.

@nbarishok
Created May 9, 2016 20:52
Show Gist options
  • Save nbarishok/eaf0657c618d41e6fbfc81e0f23f8c87 to your computer and use it in GitHub Desktop.
Save nbarishok/eaf0657c618d41e6fbfc81e0f23f8c87 to your computer and use it in GitHub Desktop.
public class MovieStaticListFragment extends Fragment {
MovieViewModel viewModel;
CompositeSubscription subscriptions;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
if (savedInstanceState != null) {
viewModel = savedInstanceState.getParcelable(VIEW_MODEL);
}
}
@Override
public void onViewCreated() {
if (viewModel != null) {
udpateUi(viewModel);
} else {
Subscription subscription = api.getMovieList().subscribe(movieList -> {
viewModel = MovieMapper.map(movieList);
updateUi(viewModel);
}, throwable -> {
viewModel = null;
});
subscriptions.add(subscription);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putParcelable(VIEW_MODEL, viewModel);
}
@Override
public void onDestroy() {
if (!subscriptions.isUnsubscribed()) {
subscriptions.clear();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment