简体   繁体   English

如何触发onPause和onResume?

[英]How can I trigger onPause and onResume?

On the click of a button, I want onPause to be called. 在单击按钮时,我希望调用onPause。 On clicking back/after a time interval, I want onResume to be called. 在单击返回/在一个时间间隔后,我希望调用onResume。

I find that system alerts, dialogs etc. do not call onPause and onResume on show and cancel. 我发现系统警报,对话框等不会在显示和取消时调用onPause和onResume。

I need to do this to verify a functional test. 我需要执行此操作以验证功能测试。 If i show a transparent activity, it's lifecycle methods would be called which would contradict what I am trying to test. 如果我显示透明活动,则将调用其生命周期方法,这将与我要测试的内容相矛盾。

I only want to verify onPause and onResume on my current activity. 我只想在当前活动中验证onPause和onResume。 Is this possible? 这可能吗?

You need to create a new transparent (or partially transparent) Activity to trigger onPause . 您需要创建一个新的透明(或部分透明)的Activity来触发onPause Simply adding a View won't do anything. 仅仅添加一个View不会做任何事情。

Then, hitting back, or finish -ing the newly created transparent Activity will trigger the onResume method. 然后,回弹或finish新创建的透明Activity将触发onResume方法。

If you want to test onStop functionality in addition to onPause , you'll want your new Activity to be opaque and the full screen size. 如果除了onPause之外还想测试onStop功能,您将希望新的Activity是不透明的并且具有全屏大小。

You should test it this way -- let the Android system trigger these methods to be called, instead of manually running them. 您应该以这种方式进行测试-让Android系统触发这些方法的调用,而不是手动运行它们。 Hence, you can let a button click launch the new Activity , then either hit back to close it, or add a button to the new Activity to finish it. 因此,您可以让一个按钮单击以启动新的Activity ,然后单击以将其关闭以将其关闭,或者将按钮添加到新的Activityfinish它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM