1、Activity中報異常之后導(dǎo)致應(yīng)用崩潰嗎?
A: 肯定會發(fā)生崩潰啊,自己上手一實驗,驚奇的發(fā)現(xiàn)ActivityA啟動ActivityB后,ActivityB拋異常后,ActivityA居然還活著?這是什么情況?
經(jīng)過一番百度發(fā)現(xiàn),這是Activity的崩潰恢復(fù)
首先要知道ActivityB報錯之后,該App的進(jìn)程絕壁會被Kill掉的,這是默認(rèn)的異常處理器,看finally
private static class UncaughtHandler implements Thread.UncaughtExceptionHandler {
public void uncaughtException(Thread t, Throwable e) {
try {
// Don't re-enter -- avoid infinite loops if crash-reporting crashes.
if (mCrashing) return;
mCrashing = true;
if (mApplicationObject == null) {
Clog_e(TAG, "*** FATAL EXCEPTION IN SYSTEM PROCESS: " + t.getName(), e);
} else {
StringBuilder message = new StringBuilder();
message.append("FATAL EXCEPTION: ").append(t.getName()).append("\n");
final String processName = ActivityThread.currentProcessName();
if (processName != null) {
message.append("Process: ").append(processName).append(", ");
}
message.append("PID: ").append(Process.myPid());
Clog_e(TAG, message.toString(), e);
}
// Try to end profiling. If a profiler is running at this point, and we kill the
// process (below), the in-memory buffer will be lost. So try to stop, which will
// flush the buffer. (This makes method trace profiling useful to debug crashes.)
if (ActivityThread.currentActivityThread() != null) {
ActivityThread.currentActivityThread().stopProfiling();
}
// Bring up crash dialog, wait for it to be dismissed
ActivityManagerNative.getDefault().handleApplicationCrash(
mApplicationObject, new ApplicationErrorReport.CrashInfo(e));
} catch (Throwable t2) {
if (t2 instanceof DeadObjectException) {
// System process is dead; ignore
} else {
try {
Clog_e(TAG, "Error reporting crash", t2);
} catch (Throwable t3) {
// Even Clog_e() fails! Oh well.
}
}
} finally {
// Try everything to make sure this process goes away.
Process.killProcess(Process.myPid());
System.exit(10);
}
}
}
系統(tǒng)會重新新建一個進(jìn)程,僅將棧頂?shù)腁ctivity進(jìn)行恢復(fù),調(diào)用onCreate(),onRestoreSavaInstance()等等一系列操作。
PS:如果Activity運行在其他進(jìn)程當(dāng)中,發(fā)生崩潰,并不會導(dǎo)致主進(jìn)程的崩潰。還是見finally中代碼。。。。
2、為什么要用Glide,Glide的優(yōu)勢
我TM只會用啊。。。。
Glide支持三級緩存,支持加載Gif,bitmap為RGB_565,占用內(nèi)存較小
三級緩存:
- 內(nèi)存緩存
使用LRUCache算法,底層實現(xiàn)使用LinkedHashMap,LinkedHashMap在HashMap的基礎(chǔ)上擴(kuò)展了一個雙向鏈表來記錄順序,順序可選擇是插入順序還是訪問順序。 - 硬盤緩存
- 網(wǎng)絡(luò)獲取