[翻译]用生命周期感知组件处理生命周期( 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阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,566评论 25 707
  • 刘溪,远大住工国际;国学践行23期学员,24期奉献者,六项精进299期同修【知~学习》 【日精进第29天】 《六项...
    西西_3e45阅读 137评论 0 0
  • 很倒霉的你,如果总是被拒绝怎么办。 恭喜你,又收获一次拒绝。总是被拒绝难道害怕再被拒绝一次吗。据我所知,在所有星座...
    青木川_阅读 259评论 0 0
  • 看此章节,联想到工作和生活,感觉很多时候会忽略了细节,并由此带来不良后果。比如我们的内勤工作,就是需要仔细...
    尘埃wyzh阅读 261评论 0 0
  • 距今学习前端已经有一个多月了,大概六月上旬开始看视频教程和做练习,直到现在刚做完JS进阶(其实是入门)的最后一道习...
    Portuense阅读 376评论 0 1