activityA和B A跳轉到B
1:在主配置文件中添加Bactivity,如果不想B在橫豎屏切換的時候導致activity的銷毀和重新創建;可在聲明activity中添加
android:configChanges="keyboardHidden|orientation"
[html] view plaincopy
?????????<activity?????????????android:name=".DemoActivity"?????????????android:configChanges="keyboardHidden|orientation"?????????????android:theme="@android:style/Theme.Dialog"?/>???????????
2:在A的配置文件中添加一個按鈕.和單擊事件
3:重寫A中父類的onCreate onDestroy onPause onRestart onResume onStart onStop 方法,并添加system.out的TAG
為了避免橫豎屏和以為一些異常導致activity的銷毀和數據丟失,就重寫onSaveInstanceState這個方法,該方法可以保存當前頁面因為異常導致的activity銷毀時候數據,然后在下載onCreate的時候調用者寫數據就可以了,onCreate中的參數就是onSaveInstanceState方法中放入的bundle對象
[java] view plaincopy
package?com.example.lifecycle;????import?android.app.Activity;??import?android.content.Intent;??import?android.os.Bundle;??import?android.view.View;???????????public?class?MainActivity?extends?Activity?{????????????????????@Override??????????????public?void?onCreate(Bundle?savedInstanceState)?{??????????super.onCreate(savedInstanceState);??????????setContentView(R.layout.main);??????????System.out.println("main?onCreate");??????????int?page?=?0;??????????if?(savedInstanceState?!=?null)?{????????????????????????????????????????????????????????????page?=?savedInstanceState.getInt("page");??????????}??????????System.out.println("當前:?"?+?page);??????}????????????????public?void?click(View?view)?{??????????Intent?intent?=?new?Intent(this,?DemoActivity.class);??????????startActivity(intent);??????}????????@Override??????public?void?finish()?{??????????super.finish();??????????System.out.println("main?finish");??????}????????@Override??????????????protected?void?onDestroy()?{??????????super.onDestroy();??????????System.out.println("main?onDestroy");??????}????????@Override??????????????protected?void?onPause()?{??????????super.onPause();??????????System.out.println("main?onPause");??????}????????@Override??????protected?void?onRestart()?{??????????super.onRestart();??????????System.out.println("main?onRestart");??????}????????@Override??????????????protected?void?onResume()?{??????????super.onResume();??????????System.out.println("main?onResume");??????}????????@Override??????????????protected?void?onStart()?{??????????super.onStart();??????????System.out.println("main?onStart");??????}????????@Override??????????????protected?void?onStop()?{??????????super.onStop();??????????System.out.println("main?onStop");??????}????????@Override????????????????protected?void?onSaveInstanceState(Bundle?outState)?{????????????????????super.onSaveInstanceState(outState);??????????outState.putInt("page",?30);??????}????}?? 4:Bactivity代碼
[java] view plaincopy
package?com.example.lifecycle;????import?android.app.Activity;??import?android.os.Bundle;????public?class?DemoActivity?extends?Activity?{????????@Override??????public?void?onCreate(Bundle?savedInstanceState)?{??????????super.onCreate(savedInstanceState);??????????setContentView(R.layout.demo);??????????System.out.println("demo?onCreate");??????}????????@Override??????public?void?finish()?{??????????super.finish();??????????System.out.println("demo?finish");??????}????????@Override??????protected?void?onDestroy()?{??????????super.onDestroy();??????????System.out.println("demo?onDestroy");??????}????????@Override??????protected?void?onPause()?{??????????super.onPause();??????????System.out.println("demo?onPause");??????}????????@Override??????protected?void?onRestart()?{??????????super.onRestart();??????????System.out.println("demo?onRestart");??????}????????@Override??????protected?void?onResume()?{??????????super.onResume();??????????System.out.println("demo?onResume");??????}????????@Override??????protected?void?onStart()?{??????????super.onStart();??????????System.out.println("demo?onStart");??????}????????@Override??????protected?void?onStop()?{??????????super.onStop();??????????System.out.println("demo?onStop");??????}??}??
?
注意onSaveInstanceState方法的作用,可以保存activity在因為異常導致銷毀時候的數據,然后傳給onCreate方法,在做處理,就可以避免數據的錯亂,
例如 在看小說的時候切換了橫豎屏,在切換的時候會調用onCreate onStart onResume 那么activity中的當前頁數就丟了,用得從新開始,如果重寫了onSaveInstanceState方法,把當前頁放到參數bundle中,在onCreate的時候,onCreate中的參數bundle就是onSaveInstanceState方法中的bundle對象,然后判斷onCreate參數bundle是否有值,如果有就取出對應的key值,如果是取出在切換前的當前頁數,在判斷不為空的時候 可以把頁數直接定位到該頁,這樣就可以接著看了
在聲明的activity中添加??android:configChanges="keyboardHidden|orientation" 屬性 可以禁止activity在橫豎屏的時候activity被重新創建或者重啟,導致數據丟失,在游戲制作中尤為重要
通過設置這個屬性可以使Activity捕捉設備狀態變化,以下是可以被識別的內容:??
CONFIG_FONT_SCALE
CONFIG_MCC
CONFIG_MNC
CONFIG_LOCALE
CONFIG_TOUCHSCREEN
CONFIG_KEYBOARD
CONFIG_NAVIGATION
CONFIG_ORIENTATION
設置方法:將下列字段用“|”符號分隔開,例如:“locale|navigation|orientation
”
| Value | Description |
| “mcc“ | The IMSI mobile country code (MCC) has changed — that is, a SIM hasbeen detected and updated the MCC.移動國家號碼,由三位數字組成,每個國家都有自己獨立的MCC,可以識別手機用戶所屬國家。 |
| “mnc“ | The IMSI mobile network code (MNC) has changed — that is, a SIM hasbeen detected and updated the MNC.移動網號,在一個國家或者地區中,用于區分手機用戶的服務商。 |
| “locale“ | The locale has changed — for example, the user has selected a new language that text should be displayed in.用戶所在地區發生變化。 |
| “touchscreen“ | The touchscreen has changed. (This should never normally happen.) |
| “keyboard“ | The keyboard type has changed — for example, the user has plugged in an external keyboard.鍵盤模式發生變化,例如:用戶接入外部鍵盤輸入。 |
| “keyboardHidden“ | The keyboard accessibility has changed — for example, the user has slid the keyboard out to expose it.用戶打開手機硬件鍵盤 |
| “navigation“ | The navigation type has changed. (This should never normally happen.) |
| “orientation“ | The screen orientation has changed — that is, the user has rotated the device.設備旋轉,橫向顯示和豎向顯示模式切換。 |
| “fontScale“ | The font scaling factor has changed — that is, the user has selected a new global font size.全局字體大小縮放發生改變 |
轉載于:https://www.cnblogs.com/xieyuan/p/3787343.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的android activity横竖屏切换,Activity重新创建问题解决!的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。