不寫無(wú)參構(gòu)造函數(shù)會(huì)發(fā)生什么?
答:會(huì)報(bào)錯(cuò)面氓,如寫了dialogfragment的有參構(gòu)造函數(shù)卻沒有寫無(wú)參構(gòu)造函數(shù)兵钮,恢復(fù)dialogfragment的時(shí)候就會(huì)崩潰。
Unable to start activity ComponentInfo{yy.free.call.phone.app/net.yolonet.yolocall.home.HomeActivity}: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment net.yolonet.yolocall.credit.CreditCheckInDialog: could not find Fragment constructor android.app.ActivityThread.performLaunchActivity
參考:Unable to instantiate fragment 問(wèn)題解決
相比較 Dialog 來(lái)說(shuō)舌界,DialogFragment 其內(nèi)嵌了一個(gè) Dialog 掘譬,并對(duì)它進(jìn)行一些靈活的管理,并且在 Activity 被異常銷毀后重建的時(shí)候呻拌,DialogFragment 也會(huì)跟著重建葱轩,單獨(dú)使用 Dialog 就不會(huì)。而且我們可以在 DialogFragment 的 onSaveInstanceState 方法中保存一些我們的數(shù)據(jù)藐握,DialogFragment 跟著 Activity 重建的時(shí)候靴拱,從 onRestoreInstanceState 中取出數(shù)據(jù),恢復(fù)頁(yè)面顯示
作者:StarkSong
鏈接:https://juejin.im/post/5c90cfeef265da612f1b926f
來(lái)源:掘金
著作權(quán)歸作者所有猾普。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán)缭嫡,非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。