[英]How to check if an Activity receives an intent sent from another activity?
I send an intent from activity1 to activity2, after clicking on a button.单击按钮后,我将意图从活动 1 发送到活动 2。 I send extras with this intent.
我带着这个意图发送额外的内容。
Using espresso, I need to test whether the activity2 received this intent, sent from activity1.使用espresso,我需要测试activity2是否收到了从activity1发送的这个意图。
I have no idea, how to do it.我不知道,该怎么做。 I have not written any code.
我没有写任何代码。
Let's say from ActivityA you click a button which starts ActivityB:假设从 ActivityA 中单击一个启动 ActivityB 的按钮:
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("MY_EXTRA", "MY EXTRA VALUE");
A test which checks that you're sending the right data to ActivityB would be:检查您是否向 ActivityB 发送正确数据的测试将是:
@Rule
public IntentsTestRule<ActivityA> intentsTestRule = new IntentsTestRule<>(ActivityA.class);
@Test
public void testIntents() {
//from ActivityA, click the button which starts the ActivityB
onView(withText("ClickMe")).perform(click());
//validate intent and check its data
intended(allOf(
toPackage("com.your.package.name"),
hasExtra("MY_EXTRA", "MY EXTRA VALUE")
));
}
Check out the examples provided by google: https://github.com/googlesamples/android-testing/tree/master/ui/espresso/IntentsBasicSample查看谷歌提供的示例: https : //github.com/googlesamples/android-testing/tree/master/ui/espresso/IntentsBasicSample
and some documentation here: https://developer.android.com/training/testing/espresso/intents.html .以及这里的一些文档: https : //developer.android.com/training/testing/espresso/intents.html 。
first use getIntent()
首先使用
getIntent()
Intent intent = getIntent();
then using intent object to receive data from another activity然后使用意图对象从另一个活动接收数据
String id = intent.getStringExtra("your key");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.