In a android library it is using
Implementation("androidx.lifecycle:lifecycle-extensions:2.2.0") {
exclude group: "android.arch.lifecycle", module: "runtime"
}
this is the usage:
// in library:
class AppLifecycleObserver implements LifecycleObserver {
protected Context mAppContext;
protected Callback mCb;
AppLifecycleObserver(@NonNull Context appContext, @NonNull Callback cb) {
mAppContext = appContext;
mCb = cb;
}
public void register() {
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
mCb.doSomething();
}
}
// in the app who uses this library will call the AppLifecycleObserver::register():
mLifecycleObserver = new AppLifecycleObserver(context, callback);
mLifecycleObserver.register();
question 1: it is said
The APIs in lifecycle-extensions have been deprecated. Instead, add dependencies for the specific Lifecycle artifacts you need.
what is the replacement for lifecycle-extensions
?
question 2:
lifecycle release note that Behavior Changes:
LifecycleRegistry now verifies that its methods are called on main thread. It was always a requirement for lifecycles of activities, fragments etc. An addition of observers from non-main threads resulted in hard to catch crashes in runtime.
The ProcessLifecycleOwner.get().getLifecycle().addObserver(this)
will cause rash if not running in main thread. How to allow the call in any thread but not crash (dont know how the library user app will make the call in what thread)?
Note: we dont own a LifecycleRegistry
here.
question from:
https://stackoverflow.com/questions/65850618/what-is-the-replacement-for-lifecycler-extensions 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…