Lifecycle-library-stable

本文介绍如何从Lifecycle Library的alpha/beta版本迁移到稳定版1.0.0,包括使用AppCompatActivity替代LifecycleActivity的方法,以及实现LifecycleOwner接口而非LifecycleRegistryOwner的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Google 最近发布了稳定Lifecyclelibrary。笔者认为:如果你正在使用alpha或者beta版本,以下内容可能对你有用。

Deprecated LifecycleActivity

在stable version 1.0.0 之前,LifeCycle Library 并没有实现 LifecycleOwner这个接口,所以我们不得不让我们项目XXXActivity继承LifecycleActivity。笔者认为这样有一个毛病:该死的侵入性

但是,在stable version 1.0.0 之后,Google可能意识到这个问题,AppCompatActivity 已经实现了LifecycleOwner 接口,这样就可以直接继承原生的AppCompatActivity,然后使用它给你带了的功能体验

AppCompatActivity be good for you?

我按照官方文档建议,替换LifecycleAvtivity ,使用AppCompatActivity,代码如下:

public class BaseActivity extends AppCompatActivity{
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        getLifecycle().addObserver(presenter);
    }
    
}
复制代码

笔者提出几点注意事项

  • support:appcompat-v7 版本一定要>=26.1.0。27.0.2 对应的lifecycle version == 1.0.3
  • presenter 必须实现 LifecycleObserver 接口

Deprecated LifecycleRegistryOwner

如果你想自定义Lifecycle owner ,你应该实现 LifecycleOwner,而不是LifecycleRegistryOwner

Just Demo

  • build.gradle

appcompat 26.1.0 依旧使用的stable version 1.0.0, 可以考虑手动添加最新版本。 笔者尝试使用exclude 将appcompat中的lifecycle去掉,发现很多库(eg:recyclerview constrait-layout fragment ...)都依赖Lifecycle, 笔者便放弃了

implementation 'com.android.support:appcompat-v7:26.1.0'
implementation "android.arch.lifecycle:runtime:1.0.3" 
annotationProcessor "android.arch.lifecycle:compiler:1.0.3" 
复制代码
  • Activity
public class BaseActivity extends AppCompatActivity{
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        getLifecycle().addObserver(presenter);
    }
    
}
复制代码
  • Observer
public class Presenter implements LifecycleObserver {
    @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
    public void onAny(LifecycleOwner owner, Lifecycle.Event event) {
        Log.w("@@@@ L41", "Presenter onAny -> " + "");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onCreate() {
        Log.w("@@@@ L41", "Presenter onCreate -> " + "");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause() {
        Log.w("@@@@ L41", "Presenter onPause -> " + "");
    }
}
复制代码
RX Library 2.75 with Delphi 2009 support (by FlexGraphics software) ====================================================================== The Set of Native Delphi Components for Borland Delphi versions 1, 2, 3, 4, 5, 6, 7, 2005, 2006, 2009 and Borland C++ Builder 1, 3, 4, 5, 6, 2006 & 2009. 100% Source Code. Last revision date Oct 12, 1999. PLEASE FOLLOW THE INSTRUCTIONS PROVIDED IN THE INSTALLATION SECTION! TABLE OF CONTENTS ----------------- Latest Changes Overview History License Agreement Installation Demonstration Programs Source Files Using GIF Images Copyright Notes NEW FOR VERSION 2.75 -------------------- Delphi 5.0 & C++Builder 4.0 Compatibility New components: TRxLoginDialog New properties, events: TFormPlacement.RegistryRoot TFormPlacement.Version TFontComboBox.UseFonts TRxDBGrid.OnTopLeftChanged TRxDBLookupCombo.DisplayValues TStrHolder.Macros, TStrHolder.OnExpandMacros RxSpin.TValueType.vtHex New routines, methods, constants: SaveClipboardToStream, LoadClipboardFromStream (clipmon.pas) AppFileName, AppVerInfo (rxverinf.pas) XorString, XorEncode, XorDecode (strutils.pas) BUG FIXES. Overview -------- This version is the result of long unactivity of RX Library authors and some imperfections and bugs of other RX adaptations to Delphi 6. The authors of this version disclaim all warranties as to this software, whether express or implied, including without limitation any implied warranties of merchantability or fitness for a particular purpose. Use under your own responsibility, but comments (even critique) in English (or in Russian) are welcome. RX Library contains a large number of components, objects and routines for Borland Delphi with full source code. This library is compatible with Borland Delphi 1, 2, 3, 4, 5, 6 and Borland C++ Builder 1, 3, 4. This collection includes over 60 native Delphi components. RX Library is a freeware product. Feel free to distribute the library as long as all files are unmodified and kep
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值