Android Jetpack学习之Lifecycle

Android Jetpack 全家桶系列(二): Lifecycle(使用篇) LifecycleOwner是一个接口,其内部只有一个方法getLifecycle(),getLifecycle方法用于获取Lifecycle,这样就可以将MyObserver添加到Lifecycle中,当Lifecycle的生命周期发生变化时,MyObserver就会观察到,或者说是感知到。官网用的是AndroidX,因为使用AndroidX,可能会产生一些迁移的问题,这里的举例就不使用AndroidX,而是使用lifecycleandroid.arch.lifecycle库,如下所示。... 阅读详情

本文旨在记录本人学习Android Jetpack的过程,如有疏漏请指出!

传送门

 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感知组件执行操作,以响应另一个组件生命周期状态的更改,例如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感知组件,您可以将依赖组件的代码从生命周期方法转移到lifecycle组件本身。

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感知组件,这些组件可以根据activity或fragment的当前生命周期状态自动调整它们的行为。

dependencies {
    def lifecycle_version = "2.0.0"

    // ViewModel and LiveData
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    // alternatively - just ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version" // For Kotlin use lifecycle-viewmodel-ktx
    // alternatively - just LiveData
    implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
    // alternatively - Lifecycles only (no ViewModel or LiveData). Some UI
    //     AndroidX libraries use this lightweight import for Lifecycle
    implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"

    annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" // For Kotlin use kapt instead of annotationProcessor
    // alternately - if using Java8, use the following instead of lifecycle-compiler
    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

    // optional - ReactiveStreams support for LiveData
    implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version" // For Kotlin use lifecycle-reactivestreams-ktx

    // optional - Test helpers for LiveData
    testImplementation "androidx.arch.core:core-testing:$lifecycle_version"
}

声明依赖项的说明。

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框架中定义的大多数应用程序组件都具有附加的生命周期。生命周期由运行在流程中的操作系统或框架代码管理。它们是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() and onStop(), which makes them difficult to maintain.

译: 尽管这个示例看起来很好,但是在实际应用程序中,您最终会有太多的调用来管理UI和其他组件,以响应生命周期的当前状态。管理多个组件会在生命周期方法中放置大量的代码,比如onStart()和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(). This can cause a race condition where the onStop() method finishes before the 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包中提供了一些类和接口,这些类和接口可以帮助您以一种灵活和隔离的方式处理这些问题。

-------------------------开始进入正题-------------------------

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 uses two main enumerations to track the lifecycle status for its associated component:

 译:Lifecycle是一个类,它保存关于组件(如activity 或fragment)生命周期状态的信息,并允许其他对象观察这个状态。

        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.

译:事件----从框架和lifecycle类中分派的生命周期事件。这些事件映射到activities和fragments的回调事件。 

State

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

 译:状态----生命周期对象跟踪的组件的当前状态。

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组件的状态。然后你可以通过调用Lifecycle类的addObserver()方法来添加一个观察者,并传递你的观察者的一个实例,如下面的例子所示:

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是一个方法接口,表示该类有一个Lifecycle。它有一个方法getLifecycle(),这个方法必须由类实现。如果您试图管理整个应用程序流程的生命周期,请参见ProcessLifecycleOwner。

This interface abstracts the ownership of a Lifecycle from individual classes, such as Fragment and AppCompatActivity, and allows writing components that work with them. Any custom application class can implement the LifecycleOwner interface. 

译:这个接口从单个类(如Fragment和AppCompatActivity)中抽象Lifecycle的所有者,并允许编写与之一起工作的组件。任何自定义应用程序类都可以实现LifecycleOwner接口。 

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

 译:实现LifecycleObserver的组件与实现LifecycleOwner的组件无缝地工作,因为所有者可以提供一个lifecycle,观察者可以注册该生命周期来观察。

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() 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,然后在onCreate()方法中初始化 Lifecycle。这允许MyLocationListener类是自给自足的,这意味着响应lifecycle 状态变化的逻辑是在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目前处于不好的状态,则避免调用某些回调。例如,如果回调函数在保存活动状态之后运行一个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类是完整的lifecycle。如果我们需要使用来自另一个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,我们建议您使用lifecycle组件。您的库客户端可以轻松地集成这些组件,而无需在客户端进行手动管理lifecycle。

Implementing a custom LifecycleOwner

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

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:

译:Fragments and Activities在支持库26.1.0和更高版本中已经实现了LifecycleOwner接口。如果你有一个自定义类,你想让它成为LifecycleOwner,你可以使用LifecycleRegistry类,但是你需要将事件转发到该类中,如下面的代码示例所示:

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

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

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

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

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

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.

 译:保持UI控制器(activities和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 or Activity 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 上下文。如果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组件可以使您在各种情况下更容易地管理生命周期。以下是一些例子:

  • 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 or Fragment, the Lifecycle's state changes to CREATEDand the ON_STOP event is dispatched when the AppCompatActivity or Fragment's onSaveInstanceState() is called.

译:当一个Lifecycle属于AppCompatActivity或Fragment时,当调用AppCompatActivity或Fragment的onSaveInstanceState()时,生命周期的状态更改为CREATED, ON_STOP事件被分派。 

When a Fragment or AppCompatActivity's state is saved via onSaveInstanceState(), 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 throws an exception if the app runs aFragmentTransaction after state is saved. See commit() for details.

译: 当通过onSaveInstanceState()保存 Fragment或AppCompatActivity的状态时,它的UI被认为是不可变的,直到调用ON_START。试图在保存状态后修改UI可能会导致应用程序导航状态不一致,这就是为什么如果应用程序在保存状态后运行一个FragmentTransaction,那么FragmentManager会抛出异常。有关详细信息,请参见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的情况下调用它的观察者来防止这种情况的发生。在幕后,它在决定调用其观察者之前调用isAtLeast()。

Unfortunately, AppCompatActivity's onStop() method is called after onSaveInstanceState(), 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() is called by the system.

译: 为了防止这个问题,版本beta2中的Lifecycle类将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() but it doesn't necessarily call 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 version 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() is called without waiting for a call to the 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 class in API level 26 and lower.

注意:为了使这个流程更简单,并提供与旧版本更好的兼容性,从版本1.0.0-rc1开始,Lifecycle对象被标记为CREATED,当调用onSaveInstanceState()而不需要等待对onStop()方法的调用时,ON_STOP被分派。这不大可能影响您的代码,但是您需要注意,因为它与API级别26或更低的Activity类中的调用顺序不匹配。

Additional resources

To learn more about handling lifecycles with lifecycle-aware components, consult the following additional resources. 

译: 要了解关于使用lifecycle-aware组件处理lifecycles的更多信息,请参考以下附加资源。

Samples

Codelabs

Blogs

 

Jetpack-Lifecycle lifecycle的设计其实非常简单,就是一个典型的观察者模式。假设让我们用自己的思路实现一个最简单的监测Activity生命周期的观察者,动手之前思考一下观察者模式必备的几个角色:**1.被观察者**,**2.观察者**,**3.调度类(管理观察者和被观察者之间关系)**。 阅读详情

相关推荐

Android-Jetpack架构组件(一)带你了解Android-Jetpack

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。我搜集整理过这几年字节跳动,以及腾讯,阿里,华为,小米等公司的面试题,把面试的要求和技术点梳理成一份大而全的“ Android架构师”面试 Xmind(实际上比预期多花了不少精力),包含。网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。它提供一致且易于使用的界面,适用于大多数Android。

2401_84121627的博客 1505

Lifecycle源码解析,让你一次学个够!

/ 今日科技快讯 /巴西副总统莫朗当地时间3日在一个视频会议上表示,巴西不担心美方的威胁和施压,不会阻止华为参与该国的5G建设竞标,华为公司掌握的技术远超其它竞争对手。/ 作...

郭霖 2266

【Android Jetpack高手日志】Lifecycle 从入门到精通

支持库 26.1.0 及更高版本中的 Fragment 和 Activity 已实现接口。如果你有一个自定义类并希望使其成为,可以使用类,但需要将事件转发到该类,代码如下:) {该 MainActivty 是实现了 Activity 类,而不是 AppcompatActivity(FragmentActivity) 类,所以需要这样的处理。我们来看看 FragmentActivity 中的源码,比如onCreate方法和onPause方法/***/······@Override···

2401_84132204的博客 942

Android架构灵魂组件Lifecycle的生命周期机制详解

前言:写作就跟生活一样,是一趟发现之旅。Lifecycle是一个具备宿主生命周期感知能力的组件。它持有组件(Activity/Fragment)生命周期状态信息,并且允许其观察者监听宿主生命周期状态变化。一、Lifecycle的使用在使用Lifecycle观察宿主状态有三种实现方式。

https://juejin.cn/user/1654096907477549/posts 2770

androidx-lifecycle-common-2.1.0.jar

androidx-lifecycle-common-2.1.0.jar

思科模拟器Cisco Packet Tracer交换机的管理配置

一、交换机的管理方式:带内管理和带外管理 通过交换机的 Console 端口管理交换机属于带外管理;这种管理方式不占用交换机的网络端口,第一次配置交换机必须利用 Console 端口进行配置。 通过 Telnet、拨号等方式属于带内管理。 二、控制台配置思科交换机 1.新建拓扑图 新建拓扑,选择console线连接交换机和PC,一端接交换机的console口,一端接PC的RS 232 口(...

qq_37992321的博客 2万+

2024年安卓最新【Android-Jetpack】Lifecycle-从入门到精通,2024年最新学习Android开发的步骤

那我们该怎么做才能做到年薪60万+呢,对于程序员来说,只有不断学习,不断提升自己的实力。我之前有篇文章提到过,感兴趣的可以看看,到底要学习哪些知识才能达到年薪60万+。通过职友集数据可以查看,以北京 Android 相关岗位为例,其中 【20k-30k】 薪酬的 Android 工程师,占到了整体从业者的 30.8%!北京 Android 工程师「工资收入水平 」今天重点内容是怎么去学,怎么提高自己的技术。1.合理安排时间2.找对好的系统的学习资料3.有老师带,可以随时解决问题。

2401_84545747的博客 1011

告别有线:用PVE直通无线网卡,打造一台真正的‘无线’Win10软路由/测试机

本文详细介绍了如何通过PVE直通无线网卡技术,构建全无线Win10软路由或测试机。从硬件选型、PVE环境配置到PCIe和USB双模直通技术,逐步指导实现Wi-Fi AP和蓝牙连接功能,为家庭实验室和小型办公环境提供灵活高效的网络解决方案。

weixin_28683689的博客 586

Android开发学习笔记——Jetpack之Lifecycle

Android开发学习笔记——Jetpack之Lifecycle概述基本使用监听Activity或是Fragment的生命周期自定义LifecycleOwner基本实现原理LifecycleOwnerLifecycle 概述 Lifecycle与LiveData以及ViewModel相同,都是谷歌推出的Jetpack中的重要一员,Lifecycle是一个生命周期感知组件,用于感知生命周期的变化帮助我们在特定时期处理相关操作。 在开发中,为了处理生命周期,一种常见的模式是在 Activity 和 Fragme

qq_36425800的博客 399

Dify 1.0+内网部署全攻略:模型插件离线安装与信创环境适配

本文详细介绍了Dify 1.0+版本在内网环境中的部署全攻略,重点解析了模型工具插件的离线安装方法与信创环境适配技巧。通过实战经验分享,帮助企业在严格的内网隔离条件下高效部署AI平台,解决插件依赖和安装难题,提升大语言模型的应用效率。

weixin_29224529的博客 273

从零到一:桌面宠物的硬件架构设计与工程实践

本文详细介绍了基于STM32F103C8T6主控平台的桌面宠物机器人硬件架构设计与工程实践。内容涵盖电源管理、舵机控制、PCB布局及系统调试等核心环节,为硬件爱好者提供从零到一的完整开发指南,助力打造稳定可靠的智能桌面伴侣。

alice7model的博客 1112

Android Jetpack Components of Lifecycle 学习笔记

Android Jetpack Components of Lifecycle 学习笔记 Android Jetpack Components of LiveData 学习笔记 Android Jetpack Components of ViewModel 学习笔记 都说天下文章一大抄。不过我不担心,我从来不抄袭别人的见解。 也有人说博客、GibHub 上 90% 的内容都是重复的。...

阿杜 495

Android Audio音量——音频类型新增(九)

在实际开发中,经常会遇到需要新增一个音频类型(usage)的需求,这里我们简单介绍一下需要修改的代码。

小旭的专栏 1570

Android Jetpack组件学习(一):LifeCycle

前言 Lifecycle是Google推出的用于处理Activity和Fragment的生命周期相关问题的组件,是一种观察者模式的结构。 使用lifeCycle能够在生命周期的拥有者和观察者之间建立一种监听关系,当拥有者的生命周期发生变化时观察者能够及时监听到并作出对应的处理,同时观察者能够非常方便的判断当前拥有者的生命周期是什么。 从上面的描述上看,显然LifeCycle的使用场景是和Activity和Fragment的生命周期相关的业务逻辑。 举例来说,在MVP结构中,Presenter需要感知到Act

cat_is_so_cute的博客 2345

风电功率预测项目技术白皮书:短期、超短期、中长期全尺度算法详解与工程落地

聚焦风电功率预测的时间尺度与算法:短期(0~72h)以XGBoost/LightGBM/CatBoost及深度学习为主,超短期(0~4h)采用ARMA+LSTM,中长期用LightGBM等,并涵盖概率预测、多气象源融合、AI大模型+物理模型混合策略、极端天气应对及工程运维要点。

算法工程师 323

学Simulink--环境感知场景实例:基于Simulink的毫米波雷达多目标跟踪仿真

本文介绍了基于Simulink的毫米波雷达多目标跟踪仿真系统,重点分析了FMCW雷达信号处理流程(Range/Doppler FFT、CFAR检测)和多目标跟踪算法(JPDA数据关联、EKF状态估计)。通过构建城市道路交叉场景,验证了系统在跟踪精度(位置误差<0.2m)、实时性(单帧处理24ms)和鲁棒性(ID切换率<5%)方面的性能优势,并提出了UKF滤波、多传感器融合等优化方向。该仿真系统为自动驾驶环境感知提供了实用的毫米波雷达跟踪解决方案。

amy_mhd的博客 92

Android Jetpack应用指南学习笔记1之Lifecycle的简单使用

AndroidJetPack应用指南学习笔记1——LifeCycle的简单使用 1.LifeCycle的引入: 在App的build.gradle目录下添加如下依赖: implementation “androidx.lifecycle:lifecycle-extensions:2.2.0” 2.创建Service服务继承LifecycleService,因为LifecycleService是Service子类,和Service使用上没什么区别 创建服务的观察者:

u012556114的博客 1801

《Python基础教程》专栏总结篇

今天给大家带来的文章是《Python基础教程》专栏总结篇,希望能对同学们有所帮助。 文章目录 1. 背景 2. 专栏亮点 3. 你的收获 4. 详细目录

weixin_43178406的博客 73万+

【android学习】Jetpack:Lifecycle的简单使用

简介 通过观察者模式的思路去对外响应生命周期变化,简单使用在数据埋点中 引包 implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0' 编写自己的MyLifeObserver 需要继承LifecycleObserver ,这里我只监听了activity的onCreate 和 onStart方法 publ...

zkq49099的专栏 272

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

Android JetPack学习笔记-LifeCycle

此类中有2个字段,观察者对象和观察者信息,他都存了什么信息呢,我们看看其构造函数中调用的getInfo方法,以下就稍微跳一些。然后通过‘下一状态’值和之前的状态值作比较,如果小于之前的值,表示是从显示到不显示,调用。获取完状态后,调用moveToState的方法,此方法有重要的一步,就是同步状态sync()的方法,然后再获取方法的参数进行判断,可以看出参数最高只能是2个。,我们点击过去发现是个接口,所以我们要去找实现类,还记得在入口处我们返回的。的状态,这个咱们从入口看到过,应该比较熟,最后在调用它的。..

FrenkX72的博客 669

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Android_Jetpack学习之路二(Lifecycle组件)

Jetpack的Lifecycle组件

liuxiangsheng911的博客 382

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Android Jetpack 全面学习 | LifeCycle 生命周期

使用生命周期感知型组件处理生命周期。 生命周期感知型组件可执行操作来响应另一个组件(如 Activity 和 Fragment)的生命周期状态的变化。这些组件有助于写出更有条理且往往更精简的代码,这样的代码更易于维护。 一种常见的模式是在 Activity 和 Fragment 的生命周期方法中实现依赖组件的操作。但是,这种模式会导致代码条理性很差而且会扩散错误。通过使用生命周期感知型组件,可以将依赖组件的代码从生命周期方法移入组件本身中。 androidx.lifecycle 软件包提供了可用于构建生命周

qq_44102588的博客 1756

Android(kotlin)JetPack系列学习——1. Lifecycle(含源码)

lifeCycle主要是用来解耦的,LifeCycle可以帮助开发者创建可感知生命周期的组件。这样,组件便能够在其内部管理自己的生命周期,从而降低模块间的耦合度,并降低内存泄漏发生的可能性。LifeCycle不只对Activity/Fragment有用,在Service和Application中也能大显身手。使用Lifecycle解耦页面与组件使用LifecycleService解耦Service与组件使用ProcessLifecycleOwner监听应用程序周期。

qq_41915623的博客 1344

最新Jetpack学习之 Lifecycle(2),2024年最新android开发基础自学

方法会根据当前状态和 mObserverMap 中的 eldest 和 newest 的状态进行对比,判断当前的状态是向前的还是向后的。的不同之处,就是前者 getLifecycle() 返回的是 LifecycleRegistry 实例,后者返回的是 Lifecycle实例。注释2: 执行注释1实例的 dispatchEvent,传入的 event 代表的是当前状态的向后状态。可以看到 Event 中的事件和 Activity 的生命周期几乎是对应的,还有一个。, 它是 Lifecycle 的实现类。

2401_84561461的博客 909

Android Jetpack架构组件实战

Android Jetpack是一套由Android官方推出的库和工具,旨在帮助开发者更快、更轻松地构建高质量的应用。它包括多个架构组件,如Lifecycle、LiveData、ViewModel等,这些组件旨在解决Android开发中的常见问题,提高开发效率。本文详细介绍了Android Jetpack架构组件的实战应用,包括Lifecycle、LiveData、ViewModel和Repository等组件。通过使用这些组件,开发者可以构建高质量、高效的应用。

2501_92486623的博客 1021

Android最全Jetpack学习之 Lifecycle(1),2024年最新android面试40题

方法会根据当前状态和 mObserverMap 中的 eldest 和 newest 的状态进行对比,判断当前的状态是向前的还是向后的。的不同之处,就是前者 getLifecycle() 返回的是 LifecycleRegistry 实例,后者返回的是 Lifecycle实例。有朋友说要是动真格的话,需要NDK以及JVM等的知识,首现**NDK并不是神秘的东西,**你跟着官方的步骤走一遍就知道什么回事了,,它实现了 Lifecycler接口,所以我们可以看看它是如何实现该接口的。这个和上面的映射表一致。

2401_84556528的博客 447
上一篇: 设置APP全屏无标题
下一篇: AndroidX遇到ButterKnife
天津小老鼠
博客等级 码龄17年 2粉丝 30原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值