2013-04-05 88 views
1

我已經爲我的應用程序中的所有活動提供了清單中的所有權限,但我的活動在旋轉手機時重新開始。重新啓動解散我的警報框,再次調用Web服務器獲取數據和活動確實保持了以前的狀態。我曾嘗試過所有可能性,但無法獲得解決方案。我該如何解決這個問題?在android中旋轉手機時警告對話框被取消

@Override 
public void onCreate(Bundle savedInstanceState) 
{  
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.station_list_layout); 

    ConstantValues.CURRENT_ACTIVITY=StationListActivity.this; 

    ConstantValues.NEAREST_PLACE_MENU_SLIDER_FLAG=false; 
    ConstantValues.MESSAGE_MENU_SLIDER_FLAG=false; 
    ConstantValues.STATION_LIST_MENU_SLIDER_FLAG=true; 

    orientation=getResources().getConfiguration().orientation; 
    userAndMessageCount=new UserAndMessageCount(); 
    ((TextView)findViewById(R.id.stationlist_route_name)).setText(ConstantValues.ROUTE_NAME); 
    list = (ListView) findViewById(R.id.myListView); 
    Cursor cursor=new UpdateLocalDatabase().getStationNameByRoute(); 
    adapter=new StationListAdapter(StationListActivity.this, cursor); 
    adapter.notifyDataSetChanged(); 

    if(!ConstantValues.STATION_LIST_LOATED) 
     userAndMessageCount.execute(); 
    else 
    { 
     Footer.setMsgCount(ConstantValues.MSG_COUNT); 
     Footer.setUserCount(ConstantValues.FAVORITE_STATION_LIST.size()); 
     list.setAdapter(adapter); 
    } 

} 
     <activity 
      android:configChanges="orientation|keyboardHidden" 
      android:screenOrientation="unspecified" 
      android:name=".LaunchingPageActivity" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity>` 
+1

http://developer.android.com/guide/topics/resources/runtime-changes.html – RobinHood 2013-04-05 05:06:14

+0

你必須要求兩種模式? – Harshid 2013-04-05 05:12:39

+0

是的,我需要兩種模式 – SathishKumar 2013-04-05 05:14:00

回答

0

這是正常的行爲。從處理運行時變化的android文檔:

「某些設備配置可能會在運行時更改(如屏幕方向,鍵盤可用性和語言),當發生此類更改時,Android會重新啓動運行Activity(onDestroy()稱爲onCreate()),重新啓動行爲旨在通過使用與新設備配置匹配的備用資源自動重新加載應用程序來幫助您的應用程序適應新的配置。

here

1

您可以嘗試以保存和使用方法來恢復活動狀態的onSaveInstanceState()和onRestoreInstanceState()。

請參閱here閱讀更多關於它。

查看問題here

編輯 例如: 如果您正在使用的WebView你可以這樣做:

@Override 
protected void onSaveInstanceState(Bundle outState) 
{ 
    super.onSaveInstanceState(outState); 
    web.saveState(outState); 
} 

@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) 
{ 
    super.onSaveInstanceState(savedInstanceState); 
    web.restoreState(savedInstanceState); 
} 

我會推薦閱讀:Android - Preventing WebView reload on Rotate
還可以閱讀:How to prevent WebView auto refresh when screen rotation

4
android:configChanges="keyboardHidden|orientation|screenSize" 

加這對您的清單中的活動元素

<activity 
      android:configChanges="orientation|keyboardHidden|screenSize" 
      android:screenOrientation="unspecified" 
      android:name=".LaunchingPageActivity" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity>` 
+1

在清單中添加screenSize – 2013-04-05 05:09:01

+0

我不知道爲什麼,我的清單不支持screenSize。 – SathishKumar 2013-04-05 05:17:12

+0

@SathishKumar只有在開發** API級別13 **或更高級別時才需要'screenSize'屬性。 – Renjith 2013-04-05 05:23:48

0

那不是一個問題,因爲視圖需要重繪活動重新開始按照新的orientation..This畫是按照Android文檔.. 反正你可以爲取向的變化onconfigChanges註冊在你的清單,並辦理在自己..

+0

我該如何處理該問題,因爲我的應用程序從網頁獲取數據。在旋轉手機時,再次撥打網頁以獲取數據,以及警報框也被解除。很多麻煩,我該如何解決這些問題。請給出一些想法謝謝 – SathishKumar 2013-04-05 05:22:50

+0

請張貼您的活動代碼.. – Akhil 2013-04-05 07:03:16

0

添加

android:configChanges="orientation|keyboardHidden"

您相應的活動,這WIL我解決你的問題。

希望它有幫助。

+0

是的,我已經給,但沒有工作。 – SathishKumar 2013-04-05 05:23:52