美文网首页Android
Android架构组件的简单介绍及使用——Lifecycle,L

Android架构组件的简单介绍及使用——Lifecycle,L

作者: 甲虫007 | 来源:发表于2017-09-24 16:04 被阅读677次

Android Architecture Components介绍

安卓架构组件是一套帮助开发者开发健壮,可测试,可维护的组件。
包含Handling Lifecycles,LiveData,ViewMode,Room.四个大方面,每个方面都可以单独使用。本次文章主要写第一个组件的简单使用。后续慢慢写完后面的内容。

Handling Lifecycles包含的内容

1.Lifecycle
2.LifecycleObserver
3.LifecycleOwner

Lifecycle简单介绍
  • 定义具有Android生命周期的对象
  • v4包的Fragment与FragmentActivity类实现了LifecycleOwner接口,通过接口里面的getLifecycle()方法获取Lifecycle对象。
  • 我们可以在自己类里面实现LifecycleOwner接口。
  • 包含事件Events与状态States

值得注意的是Android Architecture Components库还没正式发布。所以只有api 26.1.0及其之后的support library里面的Fragments和 Activities才实现了LifecycleOwner接口。如果是这之前的自定义的activities 和fragments需要实现LifecycleOwner 接口


Lifecycle events and states
LifecycleOwner简单介绍
  • A class that has an Android lifecycle. These events can be used by custom components to handle lifecycle changes without implementing any code inside the Activity or the Fragment.
  • 具有Android生命周期的接口
  • 可以处理生命周期带来的变化却不需要在Activity或者Fragment中实现任何代码。
LifecycleObserver简单介绍
  • 这是一个接口,用于将一个类标记为LifecycleObserver
  • 接口里面没有任何方法,而是依赖于 {@lOnLifecycleEvent}注释方法。

简单示例

  • 创建一个Activity与一个MyObserver类


  • MainActivity主要代码。

本例用的api版本小于26.1.0,所以MainActivity需要自己实现LifecycleOwner

import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.LifecycleRegistry;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity implements LifecycleOwner{
    //LifecycleRegistry 实现了Lifecycle 
    private LifecycleRegistry mLifecycleRegistry=new LifecycleRegistry(MainActivity.this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next);
        MyObserver myObserver = new MyObserver();
        mLifecycleRegistry.addObserver(myObserver);
    }

    @Override
    public Lifecycle getLifecycle() {
        return mLifecycleRegistry;
    }
}
  • MyObserver代码
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.OnLifecycleEvent;
import android.util.Log;

/**
 * Created by tony.zhang
 * Email: tony.zhang.Mr@foxmail.com
 */

public class MyObserver implements LifecycleObserver {
    private static final String TAG="MyObserver";

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onCreated(){
        Log.d(TAG, "onCreated: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onStart(){
        Log.d(TAG, "onStart: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume(){
        Log.d(TAG, "onResume: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause(){
        Log.d(TAG, "onPause: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onStop(){
        Log.d(TAG, "onStop: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void customMethod(){
        Log.d(TAG, "customMethod: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
    public void onAny(){//此方法可以有参数,但类型必须如两者之一(LifecycleOwner owner,Lifecycle.Event event)
        Log.d(TAG, "onAny: ");
    }
}
  • 打印消息
D/MyObserver: onCreated: 
D/MyObserver: onAny: 
D/MyObserver: onStart: 
D/MyObserver: onAny: 
D/MyObserver: onResume: 
D/MyObserver: customMethod: 
D/MyObserver: onAny: 
  • 退出Activity打印的消息
D/MyObserver: onPause: 
D/MyObserver: onAny: 
D/MyObserver: onStop: 
D/MyObserver: onAny: 
D/MyObserver: onAny: 
  • home键隐藏Activity打印消息
D/MyObserver: onPause: 
D/MyObserver: onAny: 
D/MyObserver: onStop: 
D/MyObserver: onAny: 
  • 回到Activity打印消息
D/MyObserver: onStart: 
D/MyObserver: onAny: 
D/MyObserver: onResume: 
D/MyObserver: customMethod: 
D/MyObserver: onAny: 

最后注意

allprojects {
    repositories {
        jcenter()
        maven { url 'https://maven.google.com' }
    }
}
  • 在子模块的gradle文件里添加library
   compile 'android.arch.lifecycle:extensions:1.0.0-beta1'
   compile 'android.arch.lifecycle:runtime:1.0.0'
   annotationProcessor  'android.arch.lifecycle:compiler:1.0.0-beta1'

附录传送门

官方文档: https://developer.android.com/topic/libraries/architecture/lifecycle.html#lc-bp
demo :https://github.com/Totomiz/AndroidArchitectureComponets

相关文章

网友评论

    本文标题:Android架构组件的简单介绍及使用——Lifecycle,L

    本文链接:https://www.haomeiwen.com/subject/bhdkextx.html