Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LiveData seems to not notify observers #3234

vpratfr opened this issue Jul 12, 2017 · 6 comments


None yet
8 participants
Copy link

commented Jul 12, 2017

I am running latest Robolectric within Android Studio 2.3.3

I have described the issue more in details at Stackoverflow. But after some debugging, I feel that the problem may be something from Robolectric.

Here is the test function:

fun should_redirect_to_login_when_time_is_up_after_onStart() {
    val timeUp = MutableLiveData<Boolean>()

    kodein.addConfig {
        bind<SplashViewModel>(overrides = true) with factory {
            _: BaseActivity -> mock<SplashViewModel> { on { isTimeUp() } doReturn timeUp }

    val activity = Robolectric.buildActivity(
    timeUp.value = true

    val startedIntent = shadowOf(activity).nextStartedActivity

I have another test which demonstrates that the mock is properly used by the activity ( I log the livedata instance used in onCreate and it is the same as the one from the test)


This comment has been minimized.

Copy link

commented Jul 17, 2017

Sorry its a little difficult for us to tell what is going wrong. Is it possible for you to provide a simpler repro case, ideally using Java?


This comment has been minimized.

Copy link

commented Jul 28, 2017

Got the same problem. Using latest Robolectric version 3.4.
Here is a simple example that fails test where it shouldn't:
public class TestLifecycleActivity extends LifecycleActivity {

private TestViewModel viewModel;
private Observer<String> dataChangeObserver;

protected void onCreate(@Nullable Bundle savedInstanceState) {

    viewModel = ViewModelProviders.of(this).get(TestViewModel.class);
    viewModel.getData().observe(this, dataChangeObserver);

public TestViewModel getViewModel() {
    return viewModel;

public void setDataChangeObserver(Observer<String> observer) {
    dataChangeObserver = observer;


public class TestViewModel extends AndroidViewModel {

private MutableLiveData<String> data = new MutableLiveData<>();

public TestViewModel(Application application) {

public MutableLiveData<String> getData() {
    return data;


public class LifeCycleActivityTest {

public InstantTaskExecutorRule executorTestRule = new InstantTaskExecutorRule();

Observer<String> dataChangeObserver;

public void testDataChanges() {
    ActivityController<TestLifecycleActivity> activityController = Robolectric.buildActivity(TestLifecycleActivity.class);





This comment has been minimized.

Copy link

commented Aug 1, 2017

It looks like Robolectric does not trigger the Android Lifecycle methods when it creates the activity. I was able to workaround this by calling the "handleLifecycleEvent" method on the activity.


This comment has been minimized.

Copy link

commented Feb 18, 2018

@garmstro @vpratfr I am having the same issue. but cannot solve this with the work-around as .handleLifecycleEvent() is not exposed by the latest Lifecycle object from AppCompatActivity


This comment has been minimized.

Copy link

commented Jan 17, 2019

Same with v4's FragmentActivity, there is no method handleLifecycleEvent or getLifecycle() and onChanged is not fired!


This comment has been minimized.

Copy link

commented Jun 15, 2019

Right after needing to wait for a liveData callback I am now calling shadowOf(getMainLooper()).runOneTask(); and that does the trick. This forces the pending background task to be executed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.