Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Driving the Activity Lifecycle,drivinglifecycle

Driving the Activity Lifecycle,drivinglifecycle

編輯:關於android開發

Driving the Activity Lifecycle,drivinglifecycle


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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved