簡體   English   中英

當Orientation改變時調用Onstop方法

[英]Onstop method is called when Orientation changes

當我更改Android應用程序的方向時,它會調用onStop方法然后調用onCreate。 如何避免在方向改變時調用onStop和onCreate?

這個問題一直很活躍已經有很長時間了,但我需要回答這個問題。 我有同樣的問題並找到答案。

在清單中,你應該在你的活動中添加android:configChanges=orientation|screenLayout|layoutDirection|screenSize ,它們不應該在方向更改時調用默認操作。

...
<activity android:name=".Activity"
  android:screenOrientation="portrait"
  android:configChanges="orientation|screenLayout|layoutDirection|screenSize" 
...


在你的活動中:

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
    // This overrides default action
}



原始示例:
http://android-er.blogspot.fi/2011/05/prevent-activity-restart-when-screen.html

這是默認行為:在方向更改時重新創建活動。 但您可以決定忽略此事件。 您可以在此處找到詳細信息: 如何使應用程序忽略屏幕方向更改?

你無法避免這些,那些是系統回調。 您可以在onStop中保存狀態,然后讓系統將其傳遞給onCreate,以便在屏幕方向更改后更快地恢復狀態。

另請參閱開發者頁面上的文章和文章。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM