首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >指令插入:控件生命周期

指令插入:控件生命周期
EN

Stack Overflow用户
提问于 2012-03-08 01:08:51
回答 1查看 619关注 0票数 1

如何使用工具从TestCase中控制安卓活动的生命周期?

official documentation上,声明了“生命周期控制:使用工具,您可以使用测试用例类提供的方法来启动、暂停和销毁测试中的活动。”当然,使用此测试用例时,Acitivity将在调用getActivity()时自动创建,并在每个测试用例之后停止。但是,如何在外部控制生命周期,以检查所有生命周期方法是否正确实现?

生命周期方法onActivityXXX只是帮助调用相应的方法,但不会导致活动暂停或停止。有没有人可以帮助我,告诉我我必须使用哪些方法?

有什么方法可以测试Android应用程序的生命周期实现吗?

EN

回答 1

Stack Overflow用户

发布于 2015-05-26 18:13:25

这不会让您完全控制生命周期,但它是here的示例

代码语言:javascript
复制
// Start the main activity of the application under test
    mActivity = getActivity();

    // Get a handle to the Activity object's main UI widget, a Spinner
    mSpinner = (Spinner)mActivity.findViewById(com.android.example.spinner.R.id.Spinner01);

    // Set the Spinner to a known position
    mActivity.setSpinnerPosition(TEST_STATE_DESTROY_POSITION);

    // Stop the activity - The onDestroy() method should save the state of the Spinner
    mActivity.finish();

    // Re-start the Activity - the onResume() method should restore the state of the Spinner
    mActivity = getActivity();

    // Get the Spinner's current position
    int currentPosition = mActivity.getSpinnerPosition();

    // Assert that the current position is the same as the starting position
    assertEquals(TEST_STATE_DESTROY_POSITION, currentPosition);

这使您可以对主要生命周期事件进行一些控制。因为我正在处理同样的问题,所以我正在研究robotium,它应该会有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9606048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档