其他分享
首页 > 其他分享> > Android 中高级面试题:Activity是如何实现LifecycleOwner的?

Android 中高级面试题:Activity是如何实现LifecycleOwner的?

作者:互联网

private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);

@Override

protected void onSaveInstanceState(Bundle outState) {

mLifecycleRegistry.markState(Lifecycle.State.CREATED);

super.onSaveInstanceState(outState);

}

@Override

public Lifecycle getLifecycle() {

return mLifecycleRegistry;

}

}

SupportActivity声明了mLifecycleRegistry对象,但是没有直接使用其进行生命周期的分发,而是被ReportFragment通过activity.getLifecycle()获取使用。

2、ReportFragment

SupportActivity在onCreate为自己添加了ReportFragment:

@RestrictTo(LIBRARY_GROUP)

public class SupportActivity extends Activity implements LifecycleOwner {

// …

@Override

@SuppressWarnings(“RestrictedApi”)

protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

ReportFragment.injectIfNeededIn(this);

}

// …

}

injectIfNeededIn是ReportFragment的静态方法:

public static void injectIfNeededIn(Activity activity) {

// ProcessLifecycleOwner should always correctly work and some activities may not extend

// FragmentActivity from support lib, so we use framework fragments for activities

android.app.FragmentManager manager = activity.getFragmentManager();

if (manager.findFragmentByTag(REPORT_FRAGMENT_TAG) == null) {

manager.beginTransaction().add(new ReportFragment(), REPORT_FRAGMENT_TAG).commit();

// Hopefully, we are the first to make a transaction.

manager.executePendingTransactions();

}

}

3、低版本Activity兼容Lifecycle

SupportActivity是伴随Lifecycle才出现的,android.arch.lifecycle:extensions为早期还没有继
承SupportActivity的Activity也提供了支持,通过LifecycleDispatcher实现ReportFragment的注入:

class LifecycleDispatcher {

static void init(Context context) {

if (sInitialized.getAndSet(true)) {

return;

}

((Application) context.getApplicationContext())

.registerActivityLifecycleCallbacks(new DispatcherActivityCallback());

}

static class DispatcherActivityCallback extends EmptyActivityLifecycleCallbacks {

private final FragmentCallback mFragmentCallback;

DispatcherActivityCallback() {

mFragmentCallback = new FragmentCallback();

}

@Override

public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

if (activity instanceof FragmentActivity) {

((FragmentActivity) activity).getSupportFragmentManager()

.registerFragmentLifecycleCallbacks(mFragmentCallback, true);

}

ReportFragment.injectIfNeededIn(activity);

}

}

}

之前还疑惑为什么ReportFragment的实现不写到SupportActivity中去,看到这里终于理解了其存在的意义了吧。

LifecycleDispatcher并不需要在Application中调用,他通过ContentProvider实现初始化。

public class ProcessLifecycleOwnerInitializer extends ContentProvider {

@Override

public boolean onCreate() {

LifecycleDispatcher.init(getContext());

ProcessLifecycleOwner.init(getContext());

return true;

}

}

在android.arch.lifecycle:extensionsaar的AndroidManifest中注册:

<manifest xmlns:android=“http://schemas.android.com/apk/res/android”

package=“android.arch.lifecycle.extensions” >

<provider

android:name=“android.arch.lifecycle.ProcessLifecycleOwnerInitializer”

android:authorities="${applicationId}.lifecycle-trojan"

android:exported=“false”

android:multiprocess=“true” />

${applicationId}占位符,避免authroities冲突。

可见在无侵这件事情上做到了极致,这种无侵的初始化方法非常值得我们借鉴和使用。

4、两种Fragment

通过上面分析,我们知道Activity是通过ReportFragment代理了LifecycleOwner的实现。那么在Activity中添加的LifecycleOwner与Activity的Fragment的生命周期是否一致呢?答案是否定的。

Android中存在两种Fragment有两种:

1、SDK自带的android.app.Fragment。

2、Support包中的android.support.v4.app.Fragment(AndroidX也归为此类)。

由于前者已经被@Deprecated,所以现在普遍使用的是后者,也就是Support或者AndroidX的Fragment。而出于低版本兼容性的考虑,ReportFragment是前者。
leOwner与Activity的Fragment的生命周期是否一致呢?答案是否定的。

Android中存在两种Fragment有两种:

1、SDK自带的android.app.Fragment。

2、Support包中的android.support.v4.app.Fragment(AndroidX也归为此类)。

由于前者已经被@Deprecated,所以现在普遍使用的是后者,也就是Support或者AndroidX的Fragment。而出于低版本兼容性的考虑,ReportFragment是前者。

标签:面试题,LifecycleOwner,Fragment,activity,ReportFragment,Activity,android,public
来源: https://blog.csdn.net/m0_66144992/article/details/122680393