2011-05-11 81 views

回答

6

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

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

... 
<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

+3

我不得不添加「| screenSize」以及更新的操作系統。 – 2014-02-06 19:48:09

+0

是的,基本上可以添加任何可能與方向變化有關或由方向變化引起的參數。 – 2014-02-07 20:54:15

+0

@Jack:是的,沒有它沒有工作.... – Opiatefuchs 2015-02-26 17:34:34

0

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

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

+1

可以通過調用安卓避免:configChanges = 「方向| keyboardHidden」。但是仍然存在一些問題 – indira 2011-05-12 06:53:13

+0

好吧,這顯然會阻止方向更改,但不會在方向更改(這是問題)時調用回調。 – 2012-02-25 12:25:45

+0

加1,因爲heiko絕對是賴特...... – Opiatefuchs 2015-02-26 17:35:51

2

這是默認行爲:當方向更改時重新創建活動。但是你可以決定忽略這個事件。你可以在這裏找到細節:How to make an application ignore screen orientation change?

+1

那麼,鏈接文章中的技巧顯然會阻止方向更改,但不會在方向更改(這是問題)時調用回調。 – 2012-02-25 12:26:15

相關問題