Android 面试总结 - ViewModel 是怎么保存和恢复?

为啥会调用 ActivityThread#handleRelaunchActivity 后面学到了再分析 (刚看了一遍,没看懂,看懂再说~啦啦啦)

@Override

public void handleRelaunchActivity(ActivityClientRecord tmp,

PendingTransactionActions pendingActions) {

handleRelaunchActivityInner(r, configChanges, tmp.pendingResults, tmp.pendingIntents,

pendingActions, tmp.startsNotResumed, tmp.overrideConfig, “handleRelaunchActivity”);

handleRelaunchActivityInner

private void handleRelaunchActivityInner(ActivityClientRecord r, int configChanges,

List pendingResults, List pendingIntents,

PendingTransactionActions pendingActions, boolean startsNotResumed,

Configuration overrideConfig, String reason) {

// 注意第三个参数为 true

handleDestroyActivity(r.token, false, configChanges, true, reason);

handleLaunchActivity(r, pendingActions, customIntent);

内部调用了 handleDestroyActivity,并且第三个参数 getNonConfigInstance = true handleLaunchActivity 方法后面再说

@Override

public void handleDestroyActivity(IBinder token, boolean finishing, int configChanges,

boolean getNonConfigInstance, String reason) {

ActivityClientRecord r = performDestroyActivity(token, finishing,

configChanges, getNonConfigInstance, reason);

再看 performDestroyActivity 方法 它是 Activity 销毁调用的核心实现

/** Core implementation of activity destroy call. */

ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,

int configChanges, boolean getNonConfigInstance, String reason) {

ActivityClientRecord r = mActivities.get(token);

if (r != null) {

activityClass = r.activity.getClass();

r.activity.mConfigChangeFlags |= configChanges;

if (finishing) {

r.activity.mFinished = true;

}

performPauseActivityIfNeeded(r, “destroy”);

if (!r.stopped) {

callActivityOnStop(r, false /* saveState */, “destroy”);

}

if (getNonConfigInstance) {

try {

// 重点来了

r.lastNonConfigurationInstances

= r.activity.retainNonConfigurationInstances();

} catch (Exception e) {

if (!mInstrumentation.onException(r.activity, e)) {

throw new RuntimeException(

"Unable to retain activity "

  • r.intent.getComponent().toShortString()

  • ": " + e.toString(), e);

}

}

}

重点在 performDestroyActivity 中,r.lastNonConfigurationInstances = r.activity.retainNonConfigurationInstances();

调用了 Activity 对象的 retainNonConfigurationInstances() 并将返回值赋值给了 ActivityClientRecord 类型的 r 对象的 lastNonConfigurationInstances 属性。

再看看 Activity#retainNonConfigurationInstances 做了啥:

NonConfigurationInstances retainNonConfigurationInstances() {

// 重点

Object activity = onRetainNonConfigurationInstance();

HashMap<String, Object> children = onRetainNonConfigurationChildInstances();

FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();

// We’re already stopped but we’ve been asked to retain.

// Our fragments are taken care of but we need to mark the loaders for retention.

// In order to do this correctly we need to restart the loaders first before

// handing them off to the next activity.

mFragments.doLoaderStart();

mFragments.doLoaderStop(true);

ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();

if (activity == null && children == null && fragments == null && loaders == null

&& mVoiceInteractor == null) {

return null;

}

NonConfigurationInstances nci = new NonConfigurationInstances();

nci.activity = activity;

nci.children = children;

nci.fragments = fragments;

nci.loaders = loaders;

if (mVoiceInteractor != null) {

mVoiceInteractor.retainInstance();

nci.voiceInteractor = mVoiceInteractor;

}

return nci;

}

retainNonConfigurationInstances 中调用了 onRetainNonConfigurationInstance() 。 到这儿,知道了 onRetainNonConfigurationInstance() 是怎么调用的了。

回头再看看 handleRelaunchActivityInner 中最后调用了 handleLaunchActivity熟悉Activity 启动流程的应该知道 handleLaunchActivity 是启动 Activity 的重要步骤

private void handleRelaunchActivityInner(ActivityClientRecord r, int configChanges,

List pendingResults, List pendingIntents,

PendingTransactionActions pendingActions, boolean startsNotResumed,

Configuration overrideConfig, String reason) {

// 注意第三个参数为 true

handleDestroyActivity(r.token, false, configChanges, true, reason);

handleLaunchActivity(r, pendingActions, customIntent);

}

handleLaunchActivity

@Override

public Activity handleLaunchActivity(ActivityClientRecord r,

PendingTransactionActions pendingActions, Intent customIntent) {

final Activity a = performLaunchActivity(r, customIntent);

}

performLaunchActivity 启动 Activity 的核心实现

/** Core implementation of activity launch. */

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {

Activity activity = null;

try {

java.lang.ClassLoader cl = appContext.getClassLoader();

activity = mInstrumentation.newActivity(

cl, component.getClassName(), r.intent);

StrictMode.incrementExpectedActivityCount(activity.getClass());

r.intent.setExtrasClassLoader(cl);

r.intent.prepareToEnterProcess();

if (r.state != null) {

r.state.setClassLoader(cl);

}

} catch (Exception e) {

if (!mInstrumentation.onException(activity, e)) {

throw new RuntimeException(

"Unable to instantiate activity " + component

  • ": " + e.toString(), e);

}

}

activity.attach(appContext, this, getInstrumentation(), r.token,

r.ident, app, r.intent, r.activityInfo, title, r.parent,

r.embeddedID, r.lastNonConfigurationInstances, config,

r.referrer, r.voiceInteractor, window, r.configCallback,

r.assistToken);

创建了 Activity 实例并调用了 activityattach 方法,注意 attach 方法有一个参数 传入了 r.lastNonConfigurationInstances ,有没有很熟悉,刚刚在 performDestroyActivity 中,r.lastNonConfigurationInstances = r.activity.retainNonConfigurationInstances();

调用了 Activity 对象的 retainNonConfigurationInstances() 并将返回值赋值给了 ActivityClientRecord 类型的 r 对象的 lastNonConfigurationInstances 属性。 已经串起来了。

@UnsupportedAppUsage

final void attach(Context context, ActivityThread aThread,

Instrumentation instr, IBinder token, int ident,

Application application, Intent intent, ActivityInfo info,

CharSequence title, Activity parent, String id,

NonConfigurationInstances lastNonConfigurationInstances,

Configuration config, String referrer, IVoiceInteractor voiceInteractor,

Window window, ActivityConfigCallback activityConfigCallback, IBinder assistToken) {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值