Lifecycle 简介
始终以来,解藕都是软件开发永恒的话题。在 Android 开发中,解藕很大水平上体现为零碎组件的生命周期与一般组件之间的解藕,因为一般组件在应用过程中须要依赖零碎组件的的生命周期。
举个例子,咱们常常须要在页面的 onCreate()办法中对组件进行初始化,而后在 onStop()中进行组件,或者在 onDestory()办法中对进行进行销毁。事实上,这样的工作十分繁琐,会让页面和页面耦合度变高,但又不得不做,因为如果不即时的开释资源,有可能会导致内存泄露。例如,上面是一个在 Activity 的不同生命周期办法中监听调用的例子,代码如下。
public class MainActivity extends AppCompatActivity {
private MyListener myListener;
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myListener = new MyListener(MainActivity.this);
}
@Override
protected void onStart() {super.onStart();
myListener.start();}
@Override
protected void onStop() {super.onStop();
myListener.stop();}
}
class MyListener {public MyListener(Context context) {...}
void start() {...}
void stop() {...}
}
尽管,代码看起来没什么问题,但在理论开发中可能会有多个组件在 Activity 的生命周期中进行回调,这样 Activity 的生命周期的办法中可能就须要编写大量的代码,这就使得它们难以保护。
咱们心愿在对组件进行治理不依赖页面的生命周期的回调办法,同时当页面生命周期产生扭转时,也可能即时的收到告诉。这在 Android 组件化和架构设计的时候体现的尤为显著。
那纠结什么是 Lifecycle 组件呢?总的来说,Lifecycle 就是具备生命周期感知能力的组件。简略的了解就是,当 Activity/Fragment 的生命周期产生变动时,Lifecycle 组件会感应相应的生命周期变动,当然咱们还能够通过应用 Lifecycle 组件来在自定义的类中治理 Activity/fragment 的生命周期。
目前,Lifecycle 生命周期组件次要由 Lifecycle、LifecycleOwner、LifecycleObserver 三个对象形成。
- Lifecycle:是一个持有组件生命周期状态与事件(如 Activity 或 Fragment)的信息的类。
- LifecycleOwner:Lifecycle 的提供者,通过实现 LifecycleOwner 接口来拜访 Lifecycle 生命周期对象。Fragment 和 FragmentActivity 类实现了 LifecycleOwner 接口,它具备拜访生命周期的 getLifecycle 办法,应用时须要在本人的类中实现 LifecycleOwner。
- LifecycleObserver:Lifecycle 观察者,能够应用 LifecycleOwner 类的 addObserver()办法进行注册,被注册后 LifecycleObserver 便能够察看到 LifecycleOwner 的生命周期事件。
Lifecycle 应用
应用 Lifecycle 进行利用开发之前,须要先在 app 的 build.gradle 文件中增加如下依赖代码。
dependencies {
def lifecycle_version = "2.2.0"
def arch_version = "2.1.0"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
// Lifecycles only (without ViewModel or LiveData)
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
// Saved state module for ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"
// Annotation processor
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
// optional - helpers for implementing LifecycleOwner in a Service
implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"
// optional - ProcessLifecycleOwner provides a lifecycle for the whole application process
implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"
// optional - ReactiveStreams support for LiveData
implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version"
// optional - Test helpers for LiveData
testImplementation "androidx.arch.core:core-testing:$arch_version"
}
官网用的是 AndroidX,因为应用 AndroidX 可能会产生一些迁徙的问题,这里的例子就不应用 AndroidX,应用 lifecycleandroid.arch.lifecycle 库即可,如下所示。
dependencies {implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
def lifecycle_version = "2.2.0"
// 蕴含 ViewModel 和 LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
// 仅仅蕴含 ViewModel
implementation "android.arch.lifecycle:viewmodel:$lifecycle_version" // For Kotlin use viewmodel-ktx
// 仅仅蕴含 LiveData
implementation "android.arch.lifecycle:livedata:$lifecycle_version"
// 仅仅蕴含 Lifecycles
implementation "android.arch.lifecycle:runtime:$lifecycle_version"
//noinspection LifecycleAnnotationProcessorWithJava8
annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version" // For Kotlin use kapt instead of annotationProcessor
// 如果用 Java8, 用于代替 compiler
implementation "android.arch.lifecycle:common-java8:$lifecycle_version"
// 可选,ReactiveStreams 对 LiveData 的反对
implementation "android.arch.lifecycle:reactivestreams:$lifecycle_version"
// 可选,LiveData 的测试
testImplementation "android.arch.core:core-testing:$lifecycle_version"
}
依照 Lifecycle 的应用流程,须要先定义观察者,并重写对应的生命周期,代码如下。
public class MyObserver implements LifecycleObserver {@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
void onResume(){Log.d(TAG, "Lifecycle call onResume");
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
void onPause(){Log.d(TAG, "Lifecycle call onPause");
}
}
而后,咱们在 onCreate()办法中增加观察者,代码如下。
getLifecycle().addObserver(new MyObserver());
残缺的代码如下所示。
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getLifecycle().addObserver(new MyObserver());
}
@Override
protected void onResume() {super.onResume();
Log.d(TAG, "onResume");
}
@Override
protected void onPause() {super.onPause();
Log.d(TAG, "onPause");
}
// 自定义观察者
public class MyObserver implements LifecycleObserver {@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
void onResume(){Log.d(TAG, "Lifecycle call onResume");
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
void onPause(){Log.d(TAG, "Lifecycle call onPause");
}
}
}
通过下面的解决后,MyObserver 就能够察看 MainActivity 的生命周期变动了。在下面的示例中,LifecycleOwner 能够了解为被观察者,MainActivity 默认实现了 LifecycleOwner 接口,也就是说 MainActivity 是被观察者。运行下面的代码,失去如下的日志。
com.xzh.androidx D/MainActivity: MainActivity onResume
com.xzh.androidx D/MainActivity: Lifecycle call onResume
com.xzh.androidx D/MainActivity: Lifecycle call onPause
com.xzh.androidx D/MainActivity: MainActivity onPause
当然,在被观察者中进行注册时,咱们还能够对代码进行拆解,写成上面的形式。
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private LifecycleRegistry registry;
private MyObserver myObserver = new MyObserver();
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();}
@Override
protected void onResume() {super.onResume();
registry.setCurrentState(Lifecycle.State.RESUMED);
}
@NonNull
@Override
public Lifecycle getLifecycle() {return registry;}
private void init() {registry = new LifecycleRegistry(this);
registry.addObserver(myObserver);
}
... // 省略 MyObserver 代码
}
在自定义的 Activity 或 Fragment 中实现 LifeCycleOwner 时,能够实现 LifecycleRegistryOwner 接口,如下所示。
public class MyFragment extends Fragment implements LifecycleRegistryOwner {LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this);
@Override
public LifecycleRegistry getLifecycle() {return lifecycleRegistry;}
}
通过示例的剖析能够发现,Android 的 Lifecycle 组件须要先创立一个观察者,当组件生命周期发生变化时,告诉观察者 LifeCycle 注解的办法做出响应。
Lifecycle 源码剖析
Lifecycle 注册流程
Lifecycle 应用两个枚举来跟踪其关联组件的生命周期状态,这两个枚举别离是 Event 和 State。
- State:Lifecycle 的生命周期所处的状态。
- Event:Lifecycle 生命周期对应的事件,这些事件会映射到 Activity 和 Fragment 中的回调事件中。
关上 lifecycle:common 库下的 Lifecycle 类,
public abstract class Lifecycle {@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@NonNull
AtomicReference<Object> mInternalScopeRef = new AtomicReference<>();
@MainThread
public abstract void addObserver(@NonNull LifecycleObserver observer);
@MainThread
public abstract void removeObserver(@NonNull LifecycleObserver observer);
@MainThread
@NonNull
public abstract State getCurrentState();
@SuppressWarnings("WeakerAccess")
public enum Event {
ON_CREATE,
ON_START,
ON_RESUME,
ON_PAUSE,
ON_STOP,
ON_DESTROY,
ON_ANY
}
@SuppressWarnings("WeakerAccess")
public enum State {
DESTROYED,
INITIALIZED,
CREATED,
STARTED,
RESUMED;
public boolean isAtLeast(@NonNull State state) {return compareTo(state) >= 0;
}
}
}
能够发现,Lifecycle 是一个抽象类,其外部不仅包含了增加和移除观察者的办法,还包含了 Event 和 State 两个枚举。并且,Event 中的事件和 Activity 的生命周期简直是对应的,除了 ON_ANY,它可用于匹配所有事件。State 与 Event 的生命周期关系的时序图如下图所示。
在 Lifecycle 抽象类中,enum 枚举定义了所有 State,各个状态都是依照固定的程序来变动的,所以 State 具备了生命周期的概念。Lifecycle 是抽象类,惟一的具体实现类为 LifecycleRegistry,源码如下。
public class LifecycleRegistry extends Lifecycle {
/**
* 一个列表,并且能够在遍历期间增加或者删除元素
* 新观察者的状态肯定是小于等于之前的观察者的
*/
private FastSafeIterableMap<LifecycleObserver, ObserverWithState> mObserverMap =
new FastSafeIterableMap<>();
/**
* 以后状态
*/
private State mState;
/**
* 以弱援用保留 LifecycleOwner,避免内存透露
*/
private final WeakReference<LifecycleOwner> mLifecycleOwner;
}
LifecycleRegistry 将事件告诉给所有观察者之前,存在一个同步的过程。这个同步的过程中,后面的观察者曾经告诉到了,前面的观察者还没被告诉,于是所有观察者之间的状态就不统一了,各观察者状态之间便产生了差别,只有第一个观察者的状态等于最初一个观察者的状态,并且等于 LifecycleRegistry 中的以后状态 mState,才阐明状态同步整个实现了。
加下来,咱们来看一下 Lifecycle 的注册流程,addObserver() 办法是注册观察者的入口,源码如下。
@Override
public void addObserver(@NonNull LifecycleObserver observer) {
State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED;
ObserverWithState statefulObserver = new ObserverWithState(observer, initialState);
ObserverWithState previous = mObserverMap.putIfAbsent(observer, statefulObserver);
if (previous != null) {return;}
LifecycleOwner lifecycleOwner = mLifecycleOwner.get();
if (lifecycleOwner == null) {return;}
boolean isReentrance = mAddingObserverCounter != 0 || mHandlingEvent;
State targetState = calculateTargetState(observer);
mAddingObserverCounter++;
while ((statefulObserver.mState.compareTo(targetState) < 0
&& mObserverMap.contains(observer))) {pushParentState(statefulObserver.mState);
statefulObserver.dispatchEvent(lifecycleOwner, upEvent(statefulObserver.mState));
popParentState();
// 从新计算状态,用于循环退出条件:直到 observer 的状态从 INITIALIZED 的状态递进到以后 LifecyleOwner 的状态
targetState = calculateTargetState(observer);
}
if (!isReentrance) {sync();
}
mAddingObserverCounter--;
}
在下面的源码中,代码的来前几行是将 state 与 observer 包装成 ObserverWithState 类型,state 的初始值为 INITIALIZED,而后存入汇合,如果 observer 之前曾经存在的话,就认定反复增加,间接返回。当增加的 observer 为新的时候,执行循环流程。接着判断了一下 isReentrance 的值,示意是否重入,即是否须要同时执行增加 addObserver()的流程或者同时有其余 Event 事件正在散发。而后,在 while 循环中,执行事件的散发逻辑。while 循环中有两个比拟重要的办法:dispatchEvent()
和 upEvent()
。
首先,咱们来看一下 dispatchEvent()
办法的源码,如下所示。
static class ObserverWithState {
State mState;
LifecycleEventObserver mLifecycleObserver;
ObserverWithState(LifecycleObserver observer, State initialState) {mLifecycleObserver = Lifecycling.lifecycleEventObserver(observer);
mState = initialState;
}
void dispatchEvent(LifecycleOwner owner, Event event) {State newState = getStateAfter(event);
mState = min(mState, newState);
// observer 的回调函数
mLifecycleObserver.onStateChanged(owner, event);
mState = newState;
}
}
而后再调用了 observer 的回调办法 onStateChanged()更新组件的状态 mState。
告诉观察者
后面咱们剖析了 Lifecycle 的注册观察者的流程,接下来咱们看一下 Lifecycle 又是如何告诉 Activity 或 Fragment 的生命周期扭转的呢?在 Android 8.0 时,FragmentActivity 继承自 SupportActivity,而在 Android 9.0,FragmentActivity 继承自 ComponentActivity。SupportActivity 和 ComponentActivity 的代码区别不大,以 ComponentActivity 来说,源码如下。
public class ComponentActivity extends androidx.core.app.ComponentActivity implements
LifecycleOwner,
ViewModelStoreOwner,
... // 省略其余代码
private final LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);
... // 省略其余代码
ReportFragment.injectIfNeededIn(this);
if (mContentLayoutId != 0) {setContentView(mContentLayoutId);
}
}
... // 省略其余代码
}
接下来,咱们看一下 ReportFragment 的源码。
public class ReportFragment extends Fragment {public static void injectIfNeededIn(Activity activity) {android.app.FragmentManager manager = activity.getFragmentManager();
if (manager.findFragmentByTag(REPORT_FRAGMENT_TAG) == null) {manager.beginTransaction().add(new ReportFragment(), REPORT_FRAGMENT_TAG).commit();
manager.executePendingTransactions();}
}
...
@Override
public void onActivityCreated(Bundle savedInstanceState) {dispatch(Lifecycle.Event.ON_CREATE);
}
@Override
public void onStart() {dispatch(Lifecycle.Event.ON_START);
}
... // 省略其余代码
private void dispatch(Lifecycle.Event event) {Activity activity = getActivity();
if (activity instanceof LifecycleRegistryOwner) {((LifecycleRegistryOwner) activity).getLifecycle().handleLifecycleEvent(event);
return;
}
if (activity instanceof LifecycleOwner) {Lifecycle lifecycle = ((LifecycleOwner) activity).getLifecycle();
if (lifecycle instanceof LifecycleRegistry) {((LifecycleRegistry) lifecycle).handleLifecycleEvent(event);
}
}
}
在下面的 ReportFragment 类中,Activity 通过注入一个没有 UI 的 ReportFragment,而后在 ReportFragment 的的生命周期回调中调用 dispathch() 办法散发生命周期状态的扭转。因为 Fragment 依赖于创立它的 Activity,所以 Fragment 的生命周期和 Activity 生命周期同步,这样就间接实现了 Lifecycle 监听 Activity 生命周期的性能。接下来,看一下是 dispatch()办法是如何散发 Event 的,源码如下。
private void dispatch(Lifecycle.Event event) {Activity activity = getActivity();
if (activity instanceof LifecycleRegistryOwner) {((LifecycleRegistryOwner) activity).getLifecycle().handleLifecycleEvent(event);
return;
}
if (activity instanceof LifecycleOwner) {Lifecycle lifecycle = ((LifecycleOwner) activity).getLifecycle();
if (lifecycle instanceof LifecycleRegistry) {((LifecycleRegistry) lifecycle).handleLifecycleEvent(event);
}
}
}
能够发现,在调用 getActivity()办法后向上转型强制转换为 LifecycleOwner,而后调用了 LifecycleRegistry 类的 handleLifecycleEvent()办法,而后逻辑又回到了 LifecycleRegistry 类中,进而将事件 Event 散发交由 LifecycleRegistry 进行解决。其中,handleLifecycleEvent(event)的实现如下所示。
public void handleLifecycleEvent(@NonNull Lifecycle.Event event) {State next = getStateAfter(event);
moveToState(next);
}
private void moveToState(State next) {
// 将 mState 更新为以后的 State
mState = next;
...
mHandlingEvent = true;
sync();
mHandlingEvent = false;
}
更新了 mState 的值之后,就调用 sync()办法,sync()办法就是依据 mState 的扭转做出同步操作,并执行散发事件,sync()办法的源码如下。
private void sync() {LifecycleOwner lifecycleOwner = mLifecycleOwner.get();
// 判断是否须要同步,没有同步则始终进行
while (!isSynced()) {
mNewEventOccurred = false;
if (mState.compareTo(mObserverMap.eldest().getValue().mState) < 0) {
// 同步并散发事件
backwardPass(lifecycleOwner);
}
Entry<LifecycleObserver, ObserverWithState> newest = mObserverMap.newest();
if (!mNewEventOccurred && newest != null
&& mState.compareTo(newest.getValue().mState) > 0) {
// 同步并散发事件
forwardPass(lifecycleOwner);
}
}
mNewEventOccurred = false;
}
private boolean isSynced() {if (mObserverMap.size() == 0) {return true;}
State eldestObserverState = mObserverMap.eldest().getValue().mState;
State newestObserverState = mObserverMap.newest().getValue().mState;
return eldestObserverState == newestObserverState && mState == newestObserverState;
}
首先,通过调用 isSynced() 办法来判断是否须要同步。isSynced()办法比较简单,次要通过比拟第一个 observer 和最初一个 observer,他们的 mState 值是否相等,相等的话则阐明同步结束,不相等的话持续同步,直到相等为止。
同时,sync()办法中会依据以后状态和 mObserverMap 中的 eldest 和 newest 的状态做比照,判断以后状态是向前还是向后,以向后为例。
private void forwardPass(LifecycleOwner lifecycleOwner) {
Iterator<Entry<LifecycleObserver, ObserverWithState>> ascendingIterator =
mObserverMap.iteratorWithAdditions();
while (ascendingIterator.hasNext() && !mNewEventOccurred) {Entry<LifecycleObserver, ObserverWithState> entry = ascendingIterator.next();
ObserverWithState observer = entry.getValue();//1
while ((observer.mState.compareTo(mState) < 0 && !mNewEventOccurred
&& mObserverMap.contains(entry.getKey()))) {pushParentState(observer.mState);
observer.dispatchEvent(lifecycleOwner, upEvent(observer.mState));//2
popParentState();}
}
}
forwardPass()办法最外围的就是获取 ObserverWithState 状态,ObserverWithState 的代码如下。
static class ObserverWithState {
State mState;
GenericLifecycleObserver mLifecycleObserver;
ObserverWithState(LifecycleObserver observer, State initialState) {mLifecycleObserver = Lifecycling.getCallback(observer);//1
mState = initialState;
}
void dispatchEvent(LifecycleOwner owner, Event event) {State newState = getStateAfter(event);
mState = min(mState, newState);
mLifecycleObserver.onStateChanged(owner, event);
mState = newState;
}
}
ObserverWithState 类包含了 State 和 GenericLifecycleObserver 成员变量,GenericLifecycleObserver 是一个接口,它继承了 LifecycleObserver 接口。
ReflectiveGenericLifecycleObserver 和 CompositeGeneratedAdaptersObserver 是 GenericLifecycleObserver 的实现类,这里次要查看 ReflectiveGenericLifecycleObserver 的 onStateChanged 办法是如何实现的,源码如下。
class ReflectiveGenericLifecycleObserver implements GenericLifecycleObserver {
private final Object mWrapped;
private final CallbackInfo mInfo;
ReflectiveGenericLifecycleObserver(Object wrapped) {
mWrapped = wrapped;
mInfo = ClassesInfoCache.sInstance.getInfo(mWrapped.getClass());
}
@Override
public void onStateChanged(LifecycleOwner source, Event event) {mInfo.invokeCallbacks(source, event, mWrapped);//1
}
}
onStateChanged()办法会调用 CallbackInfo 的 invokeCallbacks()办法,这里会用到 CallbackInfo 类,代码如下。
static class CallbackInfo {
final Map<Lifecycle.Event, List<MethodReference>> mEventToHandlers;
final Map<MethodReference, Lifecycle.Event> mHandlerToEvent;
CallbackInfo(Map<MethodReference, Lifecycle.Event> handlerToEvent) {
mHandlerToEvent = handlerToEvent;
mEventToHandlers = new HashMap<>();
for (Map.Entry<MethodReference, Lifecycle.Event> entry : handlerToEvent.entrySet()) {//1
Lifecycle.Event event = entry.getValue();
List<MethodReference> methodReferences = mEventToHandlers.get(event);
if (methodReferences == null) {methodReferences = new ArrayList<>();
mEventToHandlers.put(event, methodReferences);
}
methodReferences.add(entry.getKey());
}
}
@SuppressWarnings("ConstantConditions")
void invokeCallbacks(LifecycleOwner source, Lifecycle.Event event, Object target) {invokeMethodsForEvent(mEventToHandlers.get(event), source, event, target);//2
invokeMethodsForEvent(mEventToHandlers.get(Lifecycle.Event.ON_ANY), source, event,
target);
}
private static void invokeMethodsForEvent(List<MethodReference> handlers,
LifecycleOwner source, Lifecycle.Event event, Object mWrapped) {if (handlers != null) {for (int i = handlers.size() - 1; i >= 0; i--) {handlers.get(i).invokeCallback(source, event, mWrapped);//1
}
}
}
在 CallbackInfo 代码中,首先应用循环将 handlerToEvent 进行数据类型转换,转化为一个 HashMap,key 的值为事件,value 的值为 MethodReference。而 invokeMethodsForEvent()办法会传入 mEventToHandlers.get(event),也就是事件对应的 MethodReference 的汇合。而后,invokeMethodsForEvent 办法中会遍历 MethodReference 的汇合,调用 MethodReference 的 invokeCallback 办法。其中,MethodReference 类的代码如下。
@SuppressWarnings("WeakerAccess")
static class MethodReference {
final int mCallType;
final Method mMethod;
MethodReference(int callType, Method method) {
mCallType = callType;
mMethod = method;
mMethod.setAccessible(true);
}
void invokeCallback(LifecycleOwner source, Lifecycle.Event event, Object target) {
try {switch (mCallType) {
case CALL_TYPE_NO_ARG:
mMethod.invoke(target);
break;
case CALL_TYPE_PROVIDER:
mMethod.invoke(target, source);
break;
case CALL_TYPE_PROVIDER_WITH_EVENT:
mMethod.invoke(target, source, event);
break;
}
} catch (InvocationTargetException e) {throw new RuntimeException("Failed to call observer method", e.getCause());
} catch (IllegalAccessException e) {throw new RuntimeException(e);
}
}
...
}
MethodReference 类中有两个变量,一个是 callType,它代表调用办法的类型,另一个是 Method,它代表办法,并最终通过 invoke 对办法进行反射,通过反射对事件的对应办法进行调用。