4 回答

TA貢獻1842條經(jīng)驗 獲得超21個贊
Android 3.2及更高版本的更新:
注意:從Android 3.2(API級別13)開始,當設(shè)備在縱向和橫向之間切換時,“屏幕大小”也會更改。因此,如果要在開發(fā)API級別13或更高級別(由minSdkVersion和targetSdkVersion屬性聲明)時由于方向更改而阻止運行時重新啟動,則必須在
"screenSize"
值之外包含該"orientation"
值。也就是說,你必須申報android:configChanges="orientation|screenSize"
。但是,如果您的應(yīng)用程序的目標是API級別12或更低,那么您的活動始終會自行處理此配置更改(即使在Android 3.2或更高版本的設(shè)備上運行,此配置更改也不會重新啟動您的活動)。

TA貢獻1829條經(jīng)驗 獲得超7個贊
onCreate()
可以嘗試檢查Bundle
savedInstanceState
傳入事件以查看它是否為空,而不是試圖完全阻止被解雇。
例如,如果我有一些邏輯應(yīng)該在Activity
真正創(chuàng)建時運行,而不是在每次方向更改時運行,我只在onCreate()
該savedInstanceState
值為null 時才運行該邏輯。
否則,我仍然希望布局正確地重新繪制方向。
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game_list); if(savedInstanceState == null){ setupCloudMessaging(); }}
不確定這是否是最終的答案,但它對我有用。

TA貢獻1946條經(jīng)驗 獲得超4個贊
我做了什么...
在清單中,到活動部分,添加:
android:configChanges="keyboardHidden|orientation"
在活動的代碼中,實現(xiàn)了:
//used in onCreate() and onConfigurationChanged() to set up the UI elements
public void InitializeUI()
{
//get views from ID's
this.textViewHeaderMainMessage = (TextView) this.findViewById(R.id.TextViewHeaderMainMessage);
//etc... hook up click listeners, whatever you need from the Views
}
//Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
InitializeUI();
}
//this is called when the screen rotates.
// (onCreate is no longer called when screen rotates due to manifest, see: android:configChanges)
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
InitializeUI();
}
添加回答
舉報