Driving the Activity Lifecycle
Before Robolectric 2.2, most tests created Activities by calling constructors directly, (new MyActivity()) and then manually calling lifecycle methods such as onCreate(). Also widely used were a set of methods in ShadowActivity (for instanceShadowActivity.callOnCreate()) that are precursors to ActivityController.
It was a mess. The ActivityController is a Robolectric API that changes all of this. Its goal is to mimic how Android creates your Activities and drives them through their lifecycle.
ActivityController is a fluent API that was introduced in Robolectric 2.0 and is now required in 2.2. In addition to calling methods like onCreate(), it ensures that the internal state of the Activity is consistent with the lifecycle. This includes attaching the Activity to the Window and making system services like the LayoutInflater available.
//ActivityController是一个流畅的api 从2.2版本开始可用。除了调用像onCreate()方法之外,它保证了actvity的状态和生命周期一致。
What do I do now?
You don't generally create an ActivityController directly. Use Robolectric.buildActivity() to get started. For the most basic of tests where you simply need an initialized Activity, you can often get away with the following line:
//一般通常不会直接创建activitycontroller,而是使用Robolectric.buildActivity方法。如下:
Activity activity = Robolectric.buildActivity(MyAwesomeActivity.class).create().get();
This will create a new instance of MyAwesomeActivity and call through the life cycle to onCreate().
//这会创建一个activity的实例,然后调用到oncreate()生命周期方法
Want to check that something happens during onResume() but not onCreate()? Easy!
//想要查看onresume方法里面做了什么而不是oncreate?好办:
ActivityController controller = Robolectric.buildActivity(MyAwesomeActivity.class).create().start();
Activity activity = controller.get();
// assert that something hasn't happened
activityController.resume();
// assert it happened!
Similar methods are included for start(), pause(), stop(), and destroy(). So, if you want to test the full creation lifecycle:
//相似的方法也包括在 start(), pause(), stop(), and destroy()中
Activity activity = Robolectric.buildActivity(MyAwesomeActivity.class).create().start().resume().visible().get();
You can simulate starting the Activity with an intent:
//模拟使用一个intent来开启activity
Intent intent = new Intent(Intent.ACTION_VIEW);
Activity activity = Robolectric.buildActivity(MyAwesomeActivity.class).withIntent(intent).create().get();
... or restore saved instance state:
//或者模拟开启一个带有bundle的activity
Bundle savedInstanceState = new Bundle();
Activity activity = Robolectric.buildActivity(MyAwesomeActivity.class)
.create()
.restoreInstanceState(savedInstanceState)
.get();
Check out the ActivityController Java Docs to see more public methods available for your testing needs.
Wait, What's This visible() Nonsense?
//visible()是不是废话啊?
Turns out that in a real Android app, the view hierarchy of an Activity is not attached to the Window until sometime afteronCreate() is called. Until this happens, the Activity's views do not report as visible. This means you can't click on them (amongst other unexpected behavior). The Activity's hierarchy is attached to the Window on a device or emulator afteronPostResume() on the Activity. Rather than make assumptions about when the visibility should be updated, Robolectric puts the power in the developer's hands when writing tests.
//当oncreate方法调用了activity仍然没有visible,这意味着你不能点击(或者其他的用户行为)。hierarche被添加到window上是直到onPostresume()方法被调用,这时候才可以和用户交互。robolectric把这个更改状态的权利交到开发者手中
So when do you call it? Whenever you're interacting with the views inside the Activity. Methods like Robolectric.clickOn()require that the view is visible and properly attached in order to function. You should call visible() after create().
//所以我们什么时候调用它呢?当你要和activity中的view交互时,你应该在create之后调用visible
Driving the Activity Lifecycle的更多相关文章
- Activity Lifecycle
Activity Lifecycle Activities in the system are managed as an activity stack(activity栈?). When a new ...
- Android官方文档翻译 十六 4.Managing the Activity Lifecycle
Managing the Activity Lifecycle 管理activity的生命周期 Dependencies and prerequisites 依赖关系和先决条件 How to crea ...
- 让普通 Java 类自动感知 Activity Lifecycle
背景 在 Android 开发中,我们都很熟悉 Activity 的 Lifecycle,并且会在特定的 Lifecycle 下执行特定的操作.当然,我们清楚 Lifecycle 本身是带有 Andr ...
- android activity lifecycle
学习android的activity,之前一直没有去琢磨,今天正好了解一下activity生命周期. 参考链接: https://developer.android.com/guide/compone ...
- Android四大组件之——Activity的生命周期(图文详解)
转载请在文章开头处注明本博客网址:http://www.cnblogs.com/JohnTsai 联系方式:JohnTsai.Work@gmail.com [Andro ...
- Andoid activity 生命周期
今天介绍一下Android中最常用的组件activity的生命周期.当activity处于Android应用中运行时,它的活动状态由Android以Activity栈的形式管理.当前活动的Activi ...
- How to use Android Activity's finish(), onDestory() and System.exit(0) methods
Activity.finish() Calling this method will let the system know that the programmer wants the current ...
- 跟着Android官网学习Activity
1.Activity介绍 An Activity is an application component that provides a screen with which users can int ...
- Android学习笔记:Activity生命周期详解
进行android的开发,必须深入了解Activity的生命周期.而对这个讲述最权威.最好的莫过于google的开发文档了. 本文的讲述主要是对 http://developer.android.co ...
随机推荐
- X/Open DTP——分布式事务模型
转载:http://www.cnblogs.com/aigongsi/archive/2012/10/11/2718313.html 这一几天一直在回顾事务相关的知识,也准备把以前了解皮毛的知识进行一 ...
- python相关的工具
在使用python的时候,发现Adaconda工具包真的很不错,里面集合了很多的工具,并且,自带了很多的python常用模块 另外,PyCharm编辑器也是不错的,界面清晰,可以实现数据的可视化
- windows相关小知识
获得本机MAC1 快捷键win+R打开运行窗口, 输入cmd回车进入控制台2 输入ipconfig -all 找到本地连接中的物理地址 根据IP获得MAC方法:1 进入cmd控制台,执行:ping ...
- http server v0.1_http_reponse.c
#include <string.h> #include <sys/stat.h> #include <sys/mman.h> #include <fcntl ...
- OpenJDK与HashMap
OpenJDK的非堆JDK增强提议(JDK Enhancement-Proposal,JEP)试图标准化一项基础设施,它从Java6开始,只能在HotSpot和OpenJDK内部使用.这种设施能够像管 ...
- SecureCRT 颜色
默认的情况下,SecureCRT 是没有颜色方案的. 也就是说:用vim,你是看不到色彩显示效果,用ll 文件和文件夹也不会有颜色区别. 那如何支持颜色显示呢?方法如下: www.2cto.com ...
- 使用mysql-proxy代理实现msyql数据库读写分离
要实现读写分离,可以先看看如何实现mysql数据库主从:http://www.cnblogs.com/sustudy/p/4174189.html mysql-proxy下载地址(要看好对应版本):h ...
- TCP/IP FTP/TFTP
引言 从一台计算机向另一台计算机传送文件是在连网或互联网环境中最常见的任务.而FTP和TFTP就是这样的协议. 关于文件传输协议FTP? 端口21使用服务TCP [FTP模型] FTP连接? 1.控制 ...
- 传智播客C语言视频第二季(第一季基础上增加诸多C语言案例讲解,有效下载期为10.5-10.10关闭)
卷 backup 的文件夹 PATH 列表卷序列号为 00000025 D4A8:14B0J:.│ 1.txt│ c语言经典案例效果图示.doc│ ├─1传智播客_尹成_C语言从菜鸟到高手_第一 ...
- 【转】Java ConcurrentModificationException 异常分析与解决方案--还不错
原文网址:http://www.2cto.com/kf/201403/286536.html 一.单线程 1. 异常情况举例 只要抛出出现异常,可以肯定的是代码一定有错误的地方.先来看看都有哪些情况会 ...