[翻譯]用生命周期感知組件處理生命周期( Handling lifecycles with lifecycle-aware components)

前言:

這是一篇Android官方關于處理生命周期文檔的翻譯,我會貼出一段原文,然后在下面給出翻譯。圖片我保存到本地以后重新上傳了,這樣可以避免“引用圖片無法顯示”這類問題。翻譯的目的主要是學習這個組件的相關知識。
lifecycle-aware 我在標題里面翻譯成“生命周期感知”了,但我覺得這個詞并不準確,在正文中不翻譯,使用lifecycle-aware。
這個組件的目的是,通過將Activity或者Fragment中生命周期處理的一些代碼分離到獨立的類中,從而使代碼簡潔易于維護。用法是通過調用Activity或者Fragment的 getLifecycle().addObserver 將自定義的 LifecycleObserver 添加到 Lifecycle 中,然后就能收到生命周期的相關回調,然后在Observer中處理邏輯。


原文:

Handling lifecycles with lifecycle-aware components

譯文:

用生命周期感知組件處理生命周期

原文:

Lifecycle-aware components perform actions in response to a change in the lifecycle status of another component, such as activities and fragments. These components help you produce better-organized, and often lighter-weight code, that is easier to maintain.

譯文:

lifecycle-aware 組件執行一系列動作來響應其他組件生命周期狀態的變化,例如 activities 和 fragments,這些組件幫助你開發組織得更好,更加輕量并且更容易維護的代碼。

原文:

A common pattern is to implement the actions of the dependent components in the lifecycle methods of activities and fragments. However, this pattern leads to a poor organization of the code and to the proliferation of errors. By using lifecycle-aware components, you can move the code of dependent components out of the lifecycle methods and into the components themselves.

譯文:

一個普遍的模式是,在 activities 和 fragments 的生命周期方法中實現所依賴組件的動作。然而,這種模式導致代碼組織混亂、錯誤擴散。通過使用lifecycle-aware組件,你可以把所依賴組件的代碼從生命周期方法中移出來,移進它們自己的組件中。

原文:

The android.arch.lifecycle package provides classes and interfaces that let you build lifecycle-aware components—which are components that can automatically adjust their behavior based on the current lifecycle state of an activity or fragment.

譯文:

android.arch.lifecycle 包提供了類和接口,讓你可以構建lifecycle-aware 組件,這些組件可以根據一個Activity或者Fragment的當前生命周期狀態自動地調節它們的行為。

原文:

Note: To import android.arch.lifecycle into your Android project, see adding components to your project.

譯文:

注意:要在你的Android工程中引入 android.arch.lifecycle ,查閱 在你的項目中引入組件.

原文:

Most of the app components that are defined in the Android Framework have lifecycles attached to them. Lifecycles are managed by the operating system or the framework code running in your process. They are core to how Android works and your application must respect them. Not doing so may trigger memory leaks or even application crashes.

譯文:

Android Framework中定義的大多數app組件都附帶有生命周期。生命周期由操作系統或者運行在你的進程中的framework 代碼管理。它們是Android如何工作的核心,你的應用必須重視它們,不這樣做可能會觸發內存泄漏甚至應用崩潰。

原文:

Imagine we have an activity that shows the device location on the screen. A common implementation might be like the following:

譯文:

試想我們有一個activity在屏幕上顯示設備位置,一個普遍的實現可能像下面那樣子:

class MyLocationListener {
    public MyLocationListener(Context context, Callback callback) {
        // ...
    }

    void start() {
        // connect to system location service
    }

    void stop() {
        // disconnect from system location service
    }
}


class MyActivity extends AppCompatActivity {
    private MyLocationListener myLocationListener;

    @Override
    public void onCreate(...) {
        myLocationListener = new MyLocationListener(this, (location) -> {
            // update UI
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        myLocationListener.start();
        // manage other components that need to respond
        // to the activity lifecycle
    }

    @Override
    public void onStop() {
        super.onStop();
        myLocationListener.stop();
        // manage other components that need to respond
        // to the activity lifecycle
    }
}

原文:

Even though this sample looks fine, in a real app, you end up having too many calls that manage the UI and other components in response to the current state of the lifecycle. Managing multiple components places a considerable amount of code in lifecycle methods, such as [onStart()](https://developer.android.com/reference/android/app/Activity.html#onStart()) and [onStop()](https://developer.android.com/reference/android/app/Activity.html#onStop()), which makes them difficult to maintain.

譯文:

盡管這個例子看起來挺好,但在一個實際的APP中,你最終會有很多響應生命周期當前狀態的管理UI和其他組件的調用,在一個生命周期方法中管理多個組件產生了一份數量相當大的代碼。例如[onStart()](https://developer.android.com/reference/android/app/Activity.html#onStart())[onStop()](https://developer.android.com/reference/android/app/Activity.html#onStop()), 這使得它們難以維護。

原文:

Moreover, there's no guarantee that the component starts before the activity or fragment is stopped. This is especially true if we need to perform a long-running operation, such as some configuration check in [onStart()](https://developer.android.com/reference/android/app/Activity.html#onStart()). This can cause a race condition where the [onStop()](https://developer.android.com/reference/android/app/Activity.html#onStop()) method finishes before the [onStart()](https://developer.android.com/reference/android/app/Activity.html#onStart()), keeping the component alive longer than it's needed.

譯文:

另外,沒法保證組件在 activity 或者 fragment 的停止之前啟動。假如我需要執行一個長時間運行的操作,例如在onStart()中簽入一些配置,這可以引起一個條件競爭,onStop() 方法在 onStart() 方法之前結束,使得組件比需要存活的時間要長。

class MyActivity extends AppCompatActivity {
    private MyLocationListener myLocationListener;

    public void onCreate(...) {
        myLocationListener = new MyLocationListener(this, location -> {
            // update UI
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        Util.checkUserStatus(result -> {
            // what if this callback is invoked AFTER activity is stopped?
            if (result) {
                myLocationListener.start();
            }
        });
    }

    @Override
    public void onStop() {
        super.onStop();
        myLocationListener.stop();
    }
}

原文:

The android.arch.lifecycle package provides classes and interfaces that help you tackle these problems in a resilient and isolated way.

譯文:

[android.arch.lifecycle] (https://developer.android.com/reference/android/arch/lifecycle/package-summary.html) 包提供類和接口幫助你以一個彈性和隔離的方式處理這些問題。

原文:

Lifecycle

Lifecycle is a class that holds the information about the lifecycle state of a component (like an activity or a fragment) and allows other objects to observe this state.

譯文:

Lifecycle 是一個類,保存了組件(例如一個activity 或者一個fragment)的生命周期狀態信息,并允許其他對象觀察這個狀態。

原文:

Lifecycle uses two main enumerations to track the lifecycle status for its associated component:

譯文:

Lifecycle 使用兩個主要的枚舉來跟蹤與之關聯組件的生命周期狀態。

原文:

Event

The lifecycle events that are dispatched from the framework and the Lifecycle class. These events map to the callback events in activities and fragments.

譯文:

事件

生命周期事件從 framework 和 Lifecycle 類發出,這些事件映射到了 activities 和 fragments 中的回調事件。

原文:

State

The current state of the component tracked by the Lifecycle object.

譯文:

狀態

組件的當前狀態由Lifecycle對象跟蹤。

lifecycle-states.png

原文:

Think of the states as nodes of a graph and events as the edges between these nodes.

譯文:

把圖中的節點想成是狀態,節點之間的邊緣想成是事件。

原文:

A class can monitor the component's lifecycle status by adding annotations to its methods. Then you can add an observer by calling the addObserver() method of the Lifecycle class and passing an instance of your observer, as shown in the following example:

譯文:

通過給一個類中的方法添加注解,可以監視組件的生命周期狀態。然后你通過調用 Lifecycle 類的 addObserver() 方法添加一個 Observer,并傳遞一個你的 observer 實例,和下面的示例展示的一樣。

public class MyObserver implements LifecycleObserver {
    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void connectListener() {
        ...
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void disconnectListener() {
        ...
    }
}

myLifecycleOwner.getLifecycle().addObserver(new MyObserver());

原文:

In the example above, the myLifecycleOwner object implements the LifecycleOwner interface, which is explained in the following section.

譯文:

在上面的例子中,myLifecycleOwner 對象實現了 LifecycleOwner 接口,這個接口會在下面的部分講述。

原文:

LifecycleOwner

LifecycleOwner is a single method interface that denotes that the class has a Lifecycle. It has one method,getLifecycle(), which must be implemented by the class. If you're trying to manage the lifecycle of a whole application process instead, see ProcessLifecycleOwner.

譯文:

LifecycleOwner

LifecycleOwner 是一個單方法接口,表示這個類有一個Lifecycle對象。它有一個方法 getLifecycle(), 是類必須實現的。如果你嘗試管理整個應用進程的生命周期,查看 ProcessLifecycleOwner。

原文:

This interface abstracts the ownership of a Lifecycle from individual classes, such as [Fragment](https://developer.android.com/reference/android/support/v4/app/Fragment.html) and [AppCompatActivity](https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html), and allows writing components that work with them. Any custom application class can implement the LifecycleOwner interface.

譯文:

這個接口從個別類中抽象了 Lifecycle 的所有權,例如 Fragment 和 AppCompatActivity, 并且允許編寫和它們一起工作的組件,任何自定義的應用類都可以實現 LifecycleOwner 接口。

原文:

Components that implement LifecycleObserver work seamlessly with components that implementLifecycleOwner because an owner can provide a lifecycle, which an observer can register to watch.

譯文:

實現 LifecycleObserver 的組件和實現 LifecycleOwner 的組件無縫地工作,因為所有者可以提供一個生命周期,一個觀察者可以注冊觀察這個生命周期。

原文:

For the location tracking example, we can make the MyLocationListener class implement LifecycleObserver and then initialize it with the activity's Lifecycle in the [onCreate()](https://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)) method. This allows the MyLocationListener class to be self-sufficient, meaning that the logic to react to changes in lifecycle status is declared in MyLocationListenerinstead of the activity. Having the individual components store their own logic makes the activities and fragments logic easier to manage.

譯文:

對于位置跟蹤示例,我們可以創建 MyLocationListener 類實現 LifecycleObserver,然后在 activity 的生命周期 onCreate() 中初始化它,這使得 MyLocationListener 自給自足,意味著用 MyLocationListener 中定義的生命周期狀態變化響應邏輯替代 Activity中定義的生命周期變化。有獨立的組件保存它們自己的邏輯,使得 activities 和 fragments 邏輯容易管理。

class MyActivity extends AppCompatActivity {
    private MyLocationListener myLocationListener;

    public void onCreate(...) {
        myLocationListener = new MyLocationListener(this, getLifecycle(), location -> {
            // update UI
        });
        Util.checkUserStatus(result -> {
            if (result) {
                myLocationListener.enable();
            }
        });
  }
}

原文:

A common use case is to avoid invoking certain callbacks if the Lifecycle isn't in a good state right now. For example, if the callback runs a fragment transaction after the activity state is saved, it would trigger a crash, so we would never want to invoke that callback.

譯文:

一個常見的使用場景是避免調用特定的回調,如果 Lifecycle 現在還不是一個合適的狀態。例如,activity state 保存以后,回調運行一個 fragment 事務的會觸發一個崩潰,因此我們絕不想調用這個回調。

原文:

To make this use case easy, the Lifecycle class allows other objects to query the current state.

譯文:

為了使這個場景容易使用,Lifecycle 類允許其他對象查詢當前的狀態。

class MyLocationListener implements LifecycleObserver {
    private boolean enabled = false;
    public MyLocationListener(Context context, Lifecycle lifecycle, Callback callback) {
       ...
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    void start() {
        if (enabled) {
           // connect
        }
    }

    public void enable() {
        enabled = true;
        if (lifecycle.getCurrentState().isAtLeast(STARTED)) {
            // connect if not connected
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    void stop() {
        // disconnect if connected
    }
}

原文:

With this implementation, our LocationListener class is completely lifecycle-aware. If we need to use our LocationListener from another activity or fragment, we just need to initialize it. All of the setup and teardown operations are managed by the class itself.

譯文:

通過這個實現,我們的 LocationListener 類是完全生命周期感知的,如果我們需要在另外一個 activity 或者 fragment 使用我們的 LocationListener, 我們只需要初始化它,所有的安裝和拆除操作由類自己管理。

原文:

If a library provides classes that need to work with the Android lifecycle, we recommend that you use lifecycle-aware components. Your library clients can easily integrate those components without manual lifecycle management on the client side.

譯文:

如果一個庫提供的類需要和 Android 的生命周期一起工作,我們推薦你使用 lifecycle-aware 組件。你的庫的客戶可以很容易地集成那些組件,并且不需要在客戶端做手工的生命周期管理。

原文:

Implementing a custom LifecycleOwner

Fragments and Activities in Support Library 26.1.0 and later already implement the LifecycleOwner interface.

譯文:

實現一個自定義的 LifecycleOwner

在 Support Library 26.1.0 和以后的庫中,Fragments 和 Activities 已經實現了 LifecycleOwner 接口。

原文:

If you have a custom class that you would like to make a LifecycleOwner, you can use the LifecycleRegistry class, but you need to forward events into that class, as shown in the following code example:

譯文:

如果你想把一個自定義的的類做成一個 LifecycleOwner, 你可以使用 LifecycleRegistry 類,但是需要你向那個類分發事件,像下面的代碼示例展示的:

public class MyActivity extends Activity implements LifecycleOwner {
    private LifecycleRegistry mLifecycleRegistry;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mLifecycleRegistry = new LifecycleRegistry(this);
        mLifecycleRegistry.markState(Lifecycle.State.CREATED);
    }

    @Override
    public void onStart() {
        super.onStart();
        mLifecycleRegistry.markState(Lifecycle.State.STARTED);
    }

    @NonNull
    @Override
    public Lifecycle getLifecycle() {
        return mLifecycleRegistry;
    }
}

原文:

Best practices for lifecycle-aware components

Keep your UI controllers (activities and fragments) as lean as possible. They should not try to acquire their own data; instead, use a ViewModel to do that, and observe a LiveData object to reflect the changes back to the views.

譯文:

lifecycle-aware 組件最佳實踐

保持你的 UI 控制(activities and fragments)盡可能簡潔。他們不應該嘗試獲取它們自己的數據,可替代的,使用一個 ViewModel 做這個,并且觀察一個 LiveData 對象把變化反映到視圖。

原文:

Try to write data-driven UIs where your UI controller’s responsibility is to update the views as data changes, or notify user actions back to the ViewModel.

譯文:

嘗試編寫數據驅動的UI,你的UI控制器的職責是在數據變化時更新視圖,或者把用戶的動作回饋給 ViewModel。

原文:

Put your data logic in your ViewModel class. ViewModel should serve as the connector between your UI controller and the rest of your app. Be careful though, it isn't ViewModel's responsibility to fetch data (for example, from a network). Instead, ViewModel should call the appropriate component to fetch the data, then provide the result back to the UI controller.

譯文:

把你的數據邏輯放在 ViewModel 類中,ViewModel 像連接器一樣服務在你的UI控制器和應用的其余部分。但是要小心,獲取數據(例如,從網絡)不是 ViewModel 的職責,而是,ViewModel 應該調用合適的組件來獲取數據,然后給UI控制器提供結果。

原文:

Use Data Binding to maintain a clean interface between your views and the UI controller. This allows you to make your views more declarative and minimize the update code you need to write in your activities and fragments. If you prefer to do this in the Java programming language, use a library like Butter Knife to avoid boilerplate code and have a better abstraction.

譯文:

使用 Data Binding 在你的視圖和UI控制器之間維護一個整潔的接口,這會讓你的視圖更有說明性,并且減少你在 activities和 fragments 中需要編寫的更新代碼。如果你喜歡用java編程語言做這個,使用像 Butter Knife 這樣的庫來避免樣板化的代碼,并且有一個更好的抽象。

原文:

If your UI is complex, consider creating a presenter class to handle UI modifications. This might be a laborious task, but it can make your UI components easier to test.

譯文:

如果你的UI復雜,考慮創建一個 presenter 類來處理 UI更新,這可能是一項體力勞動,但它會使得你的UI組件容易測試。

原文:

Avoid referencing a [View](https://developer.android.com/reference/android/view/View.html) or [Activity](https://developer.android.com/reference/android/app/Activity.html) context in your ViewModel. If the ViewModel outlives the activity (in case of configuration changes), your activity leaks and isn't properly disposed by the garbage collector.

譯文:

避免在你的ViewModel中引用一個 View 或者 Activity context。如果 ViewModel 比 activity (例如配置改變) 存活時間長,你的 activity 會泄漏從而不能被垃圾回收合適地處理。

原文:

Use cases for lifecycle-aware components

Lifecycle-aware components can make it much easier for you to manage lifecycles in a variety of cases. A few examples are:

譯文:

lifecycle-aware 組件的使用場景

lifecycle-aware 組件可以使你管理各種各樣的生命周期更加簡單,幾個例子:

原文:

Switching between coarse and fine-grained location updates. Use lifecycle-aware components to enable fine-grained location updates while your location app is visible and switch to coarse-grained updates when the app is in the background. LiveData, a lifecycle-aware component, allows your app to automatically update the UI when your user changes locations.

譯文:

在大致的和精準的位置更新之間切換,使用 lifecycle-aware 組件,當你的位置應用可見時,啟用精準的位置更新,當應用在后臺時,使用大致的位置更新。LiveData 一個 lifecycle-aware 組件,可以在用戶改變位置時,讓你的應用 自動更新UI。

原文:

Stopping and starting video buffering. Use lifecycle-aware components to start video buffering as soon as possible, but defer playback until app is fully started. You can also use lifecycle-aware components to terminate buffering when your app is destroyed.

譯文:

停止和開始視頻緩沖,使用 lifecycle-aware 組件盡可能快地開始視頻緩沖,但是推遲播放到應用完全啟動。你還可以用 lifecycle-aware 組件在應用銷毀時結束緩沖。

原文:

Starting and stopping network connectivity. Use lifecycle-aware components to enable live updating (streaming) of network data while an app is in the foreground and also to automatically pause when the app goes into the background.

譯文:

開始和停止網絡連接。當應用在前臺時使用 lifecycle-aware 組件來啟用網絡數據更新,當應用進入后臺時自動暫停。

原文:

Pausing and resuming animated drawables. Use lifecycle-aware components to handle pausing animated drawables when while app is in the background and resume drawables after the app is in the foreground.

譯文:

暫停和恢復動畫,當應用在后臺時使用 lifecycle-aware 組件處理動畫暫停,并在應用到前臺以后恢復。

原文:

Handling on stop events

When a Lifecycle belongs to an [AppCompatActivity](https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html) or [Fragment](https://developer.android.com/reference/android/support/v4/app/Fragment.html), the Lifecycle's state changes to CREATEDand the ON_STOP event is dispatched when the [AppCompatActivity](https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html) or [Fragment](https://developer.android.com/reference/android/support/v4/app/Fragment.html)'s [onSaveInstanceState()](https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html#onSaveInstanceState(android.os.Bundle)) is called.

譯文:

處理on stop事件

當一個 Lifecycle 屬于一個 AppCompatActivity 或者 Fragment 的時候,Lifecycle 的狀態變成了 CREATED ,當 AppCompatActivity 或 Fragment 的 onSaveInstanceState() 被調用時,ON_STOP 事件被分發。

原文:

When a [Fragment](https://developer.android.com/reference/android/support/v4/app/Fragment.html) or [AppCompatActivity](https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html)'s state is saved via [onSaveInstanceState()](https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html#onSaveInstanceState(android.os.Bundle)), it's UI is considered immutable until ON_START is called. Trying to modify the UI after the state is saved is likely to cause inconsistencies in the navigation state of your application which is why [FragmentManager](https://developer.android.com/reference/android/support/v4/app/FragmentManager.html) throws an exception if the app runs a[FragmentTransaction](https://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html) after state is saved. See [commit()](https://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#commit()) for details.

譯文:

當一個 Fragment 或者 AppCompatActivity 的狀態通過 onSaveInstanceState() 保存以后,它的UI被認為是不可用修改的,直到 ON_START 被調用,在狀態被保存以后嘗試修改UI可能會引起應用導航狀態的不一致,這是為什么應用在狀態保存以后運行 FragmentTransaction時,FragmentManager 拋出一個異常的原因。查看[commit()](https://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#commit())了解更多。

原文:

LiveData prevents this edge case out of the box by refraining from calling its observer if the observer's associated Lifecycle isn't at least STARTED. Behind the scenes, it calls isAtLeast() before deciding to invoke its observer.

譯文:

LiveData 阻止這種邊緣情形的發生--通過阻止對它的觀察者的調用, 如果觀察者所關聯的 Lifecycle 至少不在 STARTED。在這種情形下,它在決定調用它的觀察者之前,先調用 isAtLeast() 。

原文:

Unfortunately, [AppCompatActivity](https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html)'s [onStop()](https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html#onStop()) method is called after [onSaveInstanceState()](https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html#onSaveInstanceState(android.os.Bundle)), which leaves a gap where UI state changes are not allowed but the Lifecycle has not yet been moved to the CREATED state.

譯文:

不幸的是,AppCompatActivity 的 onStop() 方法在 onSaveInstanceState() 之后調用,這在不允許 UI 狀態改變但 Lifecycle 也沒有移動到 CREATED 之間留下了缺口。

原文:

To prevent this issue, the Lifecycle class in version beta2 and lower mark the state as CREATED without dispatching the event so that any code that checks the current state gets the real value even though the event isn't dispatched until [onStop()](https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html#onStop()) is called by the system.

譯文:

為了防止這個問題,Lifecycle 類在 beta2 和更低版本把狀態標記成 CREATED, 但沒有分發事件,因此任何檢查當前狀態的代碼都會得到真正的值,盡管直到系統調用了onStop()都沒有分發事件。

原文:

Unfortunately, this solution has two major problems:

譯文:

不幸的是,這個方案有兩個主要問題:

原文:

  • On API level 23 and lower, the Android system actually saves the state of an activity even if it is partially covered by another activity. In other words, the Android system calls [onSaveInstanceState()](https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html#onSaveInstanceState(android.os.Bundle)) but it doesn't necessarily call [onStop()](https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html#onStop()). This creates a potentially long interval where the observer still thinks that the lifecycle is active even though its UI state can't be modified.

譯文:

在 API 23 及更低,Android 系統實際會在一個 Activity 被其他 Activity 部分覆蓋時保存它的狀態,換句話說,Android 系統調用 onSaveInstanceState() 但它未必會調 onStop(),這制造了一個潛在的長時間間隔,在這段時間里觀察者一直認為生命周期是活動的但UI的狀態不能被修改。

原文:

  • Any class that wants to expose a similar behavior to the LiveData class has to implement the workaround provided by Lifecycle version beta 2 and lower.

譯文:

任何類想暴露一個類似的行為給 LiveData 類,就不得不實現 Lifecycle beta 2 和更低版本提供的變通方案。

原文:

Note: To make this flow simpler and provide better compatibility with older versions, starting at version 1.0.0-rc1, Lifecycle objects are marked as CREATED and ON_STOP is dispatched when [onSaveInstanceState()](https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html#onSaveInstanceState(android.os.Bundle)) is called without waiting for a call to the [onStop()](https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html#onStop()) method. This is unlikely to impact your code but it is something you need to be aware of as it doesn't match the call order in the [Activity](https://developer.android.com/reference/android/app/Activity.html) class in API level 26 and lower.

譯文:

注意: 為了使得這個流程簡單,并且提供更好的老版本兼容,從 1.0.0-rc1 版本開始。Lifecycle 對象被標記成 CREATED ,并且當 onSaveInstanceState() 調用時不等待 onStop() 方法的調用就分發 ON_STOP。這未必影響你的代碼,但是你需要注意的事情,因為它不匹配 API 26 以及更低版本中 Activity 類的調用順序。

原文:

Additional resources

Lifecyle-aware components are part of Android Jetpack. See them in use in the Sunflower demo app.

譯文:

附加資源:

Lifecyle-aware 組件是 Android Jetpack 的一部分,在 Sunflower 示例應用中查看。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,837評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,196評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,688評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,654評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,456評論 6 406
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,955評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,044評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,195評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,725評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,608評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,802評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,318評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,048評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,422評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,673評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,424評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,762評論 2 372

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,605評論 25 708
  • 劉溪,遠大住工國際;國學踐行23期學員,24期奉獻者,六項精進299期同修【知~學習》 【日精進第29天】 《六項...
    西西_3e45閱讀 142評論 0 0
  • 很倒霉的你,如果總是被拒絕怎么辦。 恭喜你,又收獲一次拒絕。總是被拒絕難道害怕再被拒絕一次嗎。據我所知,在所有星座...
    青木川_閱讀 277評論 0 0
  • 看此章節,聯想到工作和生活,感覺很多時候會忽略了細節,并由此帶來不良后果。比如我們的內勤工作,就是需要仔細...
    塵埃wyzh閱讀 302評論 0 0
  • 距今學習前端已經有一個多月了,大概六月上旬開始看視頻教程和做練習,直到現在剛做完JS進階(其實是入門)的最后一道習...
    Portuense閱讀 392評論 0 1