robolectric 2 - create activity with intent

前端 未结 4 685
鱼传尺愫
鱼传尺愫 2021-01-04 18:55

Does creating an activity using the .withIntent() not work in Robolectric 2? I\'m doing the following

    activity = Robolectric.buildActivity(         


        
相关标签:
4条回答
  • 2021-01-04 19:43

    I figured out my problem. I wasn't instantiating the Intent properly. I was instantiating it with the no-arg constructor when i needed to give a Context and the class of the Activity it was being sent to

    0 讨论(0)
  • 2021-01-04 19:47

    For newer versions of Robolectric use Robolectric.buildActivity(Class, Intent).

    0 讨论(0)
  • 2021-01-04 19:50

    EDIT: It was fixed in version 2.2.

    I tackled with the same issue. It was reported but no fix has been provided yet. For now, I manage to hack it using Activity's setter before calling onCreate(), taking advantage from the fact that its lifecycle has not yet started:

    Intent intent = new Intent();
    MainActivity mainActivity = Robolectric.buildActivity(MainActivity.class)
                                           .create()
                                           .get();
    mainActivity.setIntent(intent);
    mainActivity.onCreate(null);
    

    0 讨论(0)
  • 2021-01-04 19:52

    This is a case where a fluent-style API kinds of leads you down the wrong path...

    You want to:

    activity = Robolectric.buildActivity(MyActivity.class)
                            .withIntent(intent)
                            .create()
                            .get();
    

    so that the intent is provided to the builder before it calls onCreate().

    0 讨论(0)
提交回复
热议问题