您好,登錄后才能下訂單哦!
方法一:
screenOrientation = application.getResources().getConfiguration().orientation;
方法二:
screenOrientation = activity.getRequestedOrientation();獲取當(dāng)前activity 的橫豎方向
動(dòng)態(tài)設(shè)置屏幕橫豎屏:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
生命周期:
橫豎屏切換
(1)無(wú)configChanges
onCreate-->
onStart-->
onResume-->
----------切換--------
onSaveInstanceState-->
onPause-->
onStop-->
onDestroy-->
onCreate-->
onStart-->
onRestoreInstanceState-->
onResume-->
-----------切換--------
onSaveInstanceState-->
onPause-->
onStop-->
onDestroy-->
onCreate-->
onStart-->
onRestoreInstanceState-->
onResume-->
onSaveInstanceState-->
onPause-->
onStop-->
onDestroy-->
onCreate-->
onStart-->
onRestoreInstanceState-->
onResume-->
(2)修改AndroidManifest.xml,把該Activity添加 android:configChanges="orientation",執(zhí)行步驟3
onSaveInstanceState-->
onPause-->
onStop-->
onDestroy-->
onCreate-->
onStart-->
onRestoreInstanceState-->
onResume-->
再執(zhí)行步驟4,發(fā)現(xiàn)不會(huì)再打印相同信息,但多打印了一行onConfigChanged
onSaveInstanceState-->
onPause-->
onStop-->
onDestroy-->
onCreate-->
onStart-->
onRestoreInstanceState-->
onResume-->
onConfigurationChanged-->
(3)改成 android:configChanges="orientation|screenSize|keyboardHidden"(sdk>13時(shí)需加上screenSize),執(zhí)行步驟3,就只打印onConfigChanged
onConfigurationChanged-->
執(zhí)行步驟4
onConfigurationChanged-->
onConfigurationChanged-->
總結(jié):
1、不設(shè)置Activity的android:configChanges時(shí),切屏?xí)匦抡{(diào)用各個(gè)生命周期,切橫屏?xí)r會(huì)執(zhí)行一次,切豎屏?xí)r會(huì)執(zhí)行兩次
2、設(shè)置Activity的android:configChanges="orientation"時(shí),切屏還是會(huì)重新調(diào)用各個(gè)生命周期,切橫、豎屏?xí)r只會(huì)執(zhí)行一次
2、設(shè)置Activity的android:configChanges="keyboardHidden"時(shí),切屏還是會(huì)重新調(diào)用各個(gè)生命周期,切橫、豎屏?xí)r只會(huì)執(zhí)行一次
3、設(shè)置Activity的android:configChanges="orientation|screenSize|keyboardHidden"時(shí),切屏不會(huì)重新調(diào)用各個(gè)生命周期,只會(huì)執(zhí)行onConfigurationChanged方法
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。