2013-05-03 109 views
0

我的Android應用程序出現問題。當我旋轉手機(onCreate被調用)時,我的活動重新開始。我有googlet,並嘗試 android:configChanges="keyboardHidden|orientation">在我的表現,但沒有運氣。可有人請向我解釋如何我能得到這個工作旋轉重新啓動活動

編輯:我發現我NEDD到廣告這些代碼

清單

<activity 
    android:name="?" 
    android:label="@string/?" 
    android:theme="@style/?" 
    android:configChanges="orientation|screenSize"> 

MainActivity

public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 

    // Checks the orientation of the screen 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); 
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ 
     Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); 
    } 
} 
+0

可以相應地重新加載旋轉數據,或將其鎖定爲縱向|景觀。 – Doomsknight 2013-05-03 21:40:34

+0

如何重新加載旋轉的數據? – Thor1401 2013-05-05 00:19:13

+0

重新啓動配置更改的活動是Android上的正確和自然行爲。只需使用onSaveInstanceState來保存您想要在旋轉過程中保留的數據。 – Teovald 2013-05-05 12:50:29

回答

1

您可以試試android:configChanges="keyboard|orientation|screenSize">。但是,請注意,在配置更改時重新啓動Activity是正常行爲。除非你有非常特殊的需求,否則你不應該使用configChanges。

2

在您的活動中,您可以覆蓋方法onSaveInstanceState。您可以將所需的信息保存到Bundle。方向更改後,該捆綁將被傳遞到onCreate方法。如果你寧願只指定一個方向使用,您的清單文件中,無論是在應用程序選項卡或活動標籤,地點:

android:screenOrientation="portrait"