了解允许你的库使用 Expo 模块 API 钩子 Android 活动和应用功能的机制。
要响应与应用相关的某些 Android 系统事件(例如入站链接和配置更改),有必要覆盖 MainActivity.java 和/或 MainApplication.java 中相应的生命周期回调。
¥To respond to certain Android system events relevant to an app, such as inbound links and configuration changes, it is necessary to override the corresponding lifecycle callbacks in MainActivity.java and/or MainApplication.java.
React Native 模块 API 不提供任何机制来钩子这些文件,因此 React Native 库的设置说明通常包括将代码复制到这些文件中的步骤。为了简化和自动化设置和维护,Expo Modules API 提供了一种机制,允许你的库挂接到 Activity
或 Application
函数。
¥The React Native module API does not provide any mechanism to hook into these, and so setup instructions for React Native libraries often include steps to copy code into these files. To simplify and automate setup and maintenance, the Expo Modules API provides a mechanism that allows your library to hook into Activity
or Application
functions.
¥Get started
首先,你需要创建一个 Expo 模块或使用 React Native 模块 API 将 Expo 模块 API 集成到库中。了解更多。
¥First, you need to have created an Expo module or integrated the Expo modules API in the library using the React Native module API. Learn more.
在模块内部,创建一个实现 Package
接口的具体类。对于大多数情况,你只需要实现 createReactActivityLifecycleListeners
或 createApplicationLifecycleListeners
方法。
¥Inside your module, create a concrete class that implements the Package
interface. For most cases, you only need to implement the createReactActivityLifecycleListeners
or createApplicationLifecycleListeners
methods.
Activity
生命周期监听器¥Activity
lifecycle listeners
你可以使用 ReactActivityLifecycleListener
钩子到 Activity
生命周期。ReactActivityLifecycleListener
使用其 ReactActivityDelegate
与 React Native 的 ReactActivity
生命周期钩子,并提供与 Android Activity
生命周期类似的体验。
¥You can hook into the Activity
lifecycle using ReactActivityLifecycleListener
. ReactActivityLifecycleListener
hooks into React Native's ReactActivity
lifecycle using its ReactActivityDelegate
and provides a similar experience to the Android Activity
lifecycle.
目前支持以下 Activity
生命周期回调:
¥The following Activity
lifecycle callbacks are currently supported:
onCreate
onResume
onPause
onDestroy
onNewIntent
onBackPressed
要创建 ReactActivityLifecycleListener
,你应该在派生的 Package
类中实现 createReactActivityLifecycleListeners
。例如,MyLibPackage
。
¥To create a ReactActivityLifecycleListener
, you should implement createReactActivityLifecycleListeners
in your derived Package
class. For example, MyLibPackage
.
// android/src/main/java/expo/modules/mylib/MyLibPackage.kt
package expo.modules.mylib
import android.content.Context
import expo.modules.core.interfaces.Package
import expo.modules.core.interfaces.ReactActivityLifecycleListener
class MyLibPackage : Package {
override fun createReactActivityLifecycleListeners(activityContext: Context): List<ReactActivityLifecycleListener> {
return listOf(MyLibReactActivityLifecycleListener())
}
}
// android/src/main/java/expo/modules/mylib/MyLibPackage.java
package expo.modules.mylib;
import android.content.Context;
import expo.modules.core.interfaces.Package;
import expo.modules.core.interfaces.ReactActivityLifecycleListener;
import java.util.Collections;
import java.util.List;
public class MyLibPackage implements Package {
@Override
public List<? extends ReactActivityLifecycleListener> createReactActivityLifecycleListeners(Context activityContext) {
return Collections.singletonList(new MyLibReactActivityLifecycleListener());
}
}
MyLibReactActivityLifecycleListener
是 ReactActivityLifecycleListener
派生类,你可以将其挂接到生命周期中。你只能重写你需要的方法。
¥MyLibReactActivityLifecycleListener
is a ReactActivityLifecycleListener
derived class that you can hook into the lifecycles. You can only override the methods you need.
// android/src/main/java/expo/modules/mylib/MyLibReactActivityLifecycleListener.kt
package expo.modules.mylib
import android.app.Activity
import android.os.Bundle
import expo.modules.core.interfaces.ReactActivityLifecycleListener
class MyLibReactActivityLifecycleListener : ReactActivityLifecycleListener {
override fun onCreate(activity: Activity, savedInstanceState: Bundle?) {
// Your setup code in `Activity.onCreate`.
doSomeSetupInActivityOnCreate(activity)
}
}
// android/src/main/java/expo/modules/mylib/MyLibReactActivityLifecycleListener.java
package expo.modules.mylib;
import android.app.Activity;
import android.os.Bundle;
import expo.modules.core.interfaces.ReactActivityLifecycleListener;
public class MyLibReactActivityLifecycleListener implements ReactActivityLifecycleListener {
@Override
public void onCreate(Activity activity, Bundle savedInstanceState) {
// Your setup code in `Activity.onCreate`.
doSomeSetupInActivityOnCreate(activity);
}
}
Application
生命周期监听器¥Application
lifecycle listeners
你可以使用 ApplicationLifecycleListener
钩子到 Application
生命周期。
¥You can hook into the Application
lifecycle using ApplicationLifecycleListener
.
目前支持以下 Application
生命周期回调:
¥The following Application
lifecycle callbacks are currently supported:
onCreate
onConfigurationChanged
要创建 ApplicationLifecycleListener
,你应该在派生的 Package
类中实现 createApplicationLifecycleListeners
。例如,MyLibPackage
。
¥To create an ApplicationLifecycleListener
, you should implement createApplicationLifecycleListeners
in your derived Package
class. For example, MyLibPackage
.
// android/src/main/java/expo/modules/mylib/MyLibPackage.kt
package expo.modules.mylib
import android.content.Context
import expo.modules.core.interfaces.ApplicationLifecycleListener
import expo.modules.core.interfaces.Package
class MyLibPackage : Package {
override fun createApplicationLifecycleListeners(context: Context): List<ApplicationLifecycleListener> {
return listOf(MyLibApplicationLifecycleListener())
}
}
// android/src/main/java/expo/modules/mylib/MyLibPackage.java
import android.content.Context;
import java.util.Collections;
import java.util.List;
import expo.modules.core.interfaces.ApplicationLifecycleListener;
import expo.modules.core.interfaces.Package;
public class MyLibPackage implements Package {
@Override
public List<? extends ApplicationLifecycleListener> createApplicationLifecycleListeners(Context context) {
return Collections.singletonList(new MyLibApplicationLifecycleListener());
}
}
MyLibApplicationLifecycleListener
是 ApplicationLifecycleListener
派生类,可以钩子到 Application
生命周期回调。你应该只重写你需要的方法(由于可能的维护成本)。
¥MyLibApplicationLifecycleListener
is an ApplicationLifecycleListener
derived class that can hook into the Application
lifecycle callbacks. You should only override the methods you need (due to possible maintenance costs).
// android/src/main/java/expo/modules/mylib/MyLibApplicationLifecycleListener.kt
package expo.modules.mylib
import android.app.Application
import expo.modules.core.interfaces.ApplicationLifecycleListener
class MyLibApplicationLifecycleListener : ApplicationLifecycleListener {
override fun onCreate(application: Application) {
// Your setup code in `Application.onCreate`.
doSomeSetupInApplicationOnCreate(application)
}
}
// android/src/main/java/expo/modules/mylib/MyLibApplicationLifecycleListener.java
package expo.modules.mylib;
import android.app.Application;
import expo.modules.core.interfaces.ApplicationLifecycleListener;
public class MyLibApplicationLifecycleListener implements ApplicationLifecycleListener {
@Override
public void onCreate(Application application) {
// Your setup code in `Application.onCreate`.
doSomeSetupInApplicationOnCreate(application);
}
}
¥Known issues
onStart
和 onStop
Activity 监听器¥Why there are no onStart
and onStop
Activity listeners
在当前的实现中,我们没有从 MainActivity
设置钩子,而是从 ReactActivityDelegate
设置钩子。MainActivity
和 ReactActivityDelegate
之间存在一些细微差别。由于 ReactActivityDelegate
没有 onStart
和 onStop
,所以我们这里还不支持它们。
¥In the current implementation, we do not set up the hooks from MainActivity
but from ReactActivityDelegate
. There are some slight differences between MainActivity
and ReactActivityDelegate
. Since ReactActivityDelegate
does not have onStart
and onStop
, we don't yet support them here.
¥Interface stability
监听器接口可能会在 Expo SDK 版本之间不时发生变化。我们的向后兼容性策略始终是添加新接口并为我们计划删除的接口添加 @Deprecated
注释。我们的接口都是基于 Java 8 接口默认方法;你不必也不应该实现所有方法。这样做将有利于你的模块在 Expo SDK 之间的维护成本。
¥The listener interfaces may change from time to time between Expo SDK releases. Our strategy for backward compatibility is always to add new interfaces and add @Deprecated
annotation for interfaces we plan to remove. Our interfaces are all based on Java 8 interface default method; you don't have to and should not implement all methods. Doing this will benefit your module's maintenance cost between Expo SDKs.