如何判断 Android 活动何时完成加载? [英] how can you tell when an Android activity is finished loading?

查看:36
本文介绍了如何判断 Android 活动何时完成加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我们的 Android 应用开发自动化测试套件,但在等待活动完全加载时遇到了问题.我可以调用 getActivity,但仅仅因为它显示了我希望在我的测试中看到的活动并不总是意味着活动的组件可以使用(完全加载).查看 Activity API 没有发现任何问题,其他方法似乎太具有侵入性并且破坏了测试的初始状态.有谁知道是否有办法询问应用或虚拟机当前活动是否已加载?

I'm in the process of working on an automated test suite for our android app, and running into trouble waiting for activities to fully load. I can call getActivity, but just because it shows the activity that I'm hoping to see in my test doesn't always seem to mean that the activity's components are ready for use (fully loaded). Looking through the Activity API didn't turn anything up, and other methods seem too invasive and have spoiled the tests initial state. Does anyone know if there's a way to ask the app or the VM if the current activity is loaded?

推荐答案

正如我在 注释,在 onCreate() 早期调用 setContentView() 后,您的视图层次结构应该可以正常工作.我从来没有在任何活动或测试课上遇到过这样的问题..

As I mentioned in a comment, your view hierarchy should be working after your call to setContentView() early in onCreate(). I've never had any problems like this with any activity or test class..

我不确定这对这种特定情况是否有帮助,但通常您可以通过调用 getInstrumentation().waitForIdleSync().这将一直阻塞,直到没有更多的 UI 事件需要处理.

I'm not sure this is of any help for this specific case, but in general you can determine when the UI event queue is empty by calling getInstrumentation().waitForIdleSync(). That'll block until there's no more UI events to process.

这篇关于如何判断 Android 活动何时完成加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆