2013-02-27 70 views
0

我的應用需要從服務器獲取數據。所以我在ProgressDialogonPreExecute()方法和doInBackground()方法顯示數據從服務器加載。如何在方向改變時避免強制關閉

此時如果我改變方向,應用程序組合關閉。因爲當前活動已被破壞,並且doInBackground()可能仍會引用舊活動。所以,我提到了this post,它討論了同樣的問題。但我不想使用android:configchanges,因爲它不是首選。 androiddevelopersite說這應該是最後的手段,而不是大多數應用程序的首選。

那麼,有人可以請建議與必要的代碼片段如何處理這種情況下,我的應用程序不強制關閉?

Eidt:我的代碼

public class DataListActivity extends BaseActivity { 

     private ArrayList<String> valueslist; 

     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.listlayout); 
        new LoadAsync().execute(); 

     } 


     class LoadAsync extends AsyncTask<String, String, String> { 

       /** 
       * Before starting background thread Show Progress Dialog 
       * */ 
       @Override 
       protected void onPreExecute() { 
        super.onPreExecute(); 
        progressDialog = new ProgressDialog(DataListActivity.this); 
        progressDialog.setMessage("Loading..."); 
        progressDialog.setIndeterminate(false); 
        progressDialog.setCancelable(false); 
        progressDialog.show(); 
       } 

       /** 
       * getting All datafrom url 
       * */ 
       protected String doInBackground(String... args) { 

        //Here I am doing httppost request. 
          try{ 
          // looping through All data that I got from the server 
          for (int i = 0; i < subCategoriesJson.length(); i++) { 
           JSONObject jsonobj = subCategoriesJson.getJSONObject(i); 

           // Storing each json item in variable 
           String item = jsonobj.getString("data"); 

           valueslist.add(item); 

          } 
          return "1"; 
         }      
         else { 

          return null; 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
        return null; 
       } 

    protected void onPostExecute(String msg) { 

        if(msg != null && msg.equals("1")) 
        { 

        progressDialog.dismiss(); 

        runOnUiThread(new Runnable() { 

         public void run() { 
          //Updating parsed json data to Listview 

          ListView listView = (ListView)findViewById(R.id.list1); 

      ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item, valueslist); 
           listView.setAdapter(arrayAdapter); 

      listView.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
          public void onItemClick(AdapterView<?> parent, View view, 
              int position, long id) { 


           String selectedthing = valueslist.get(position); 

            } 
           }); 

         } 
        }); 
       }else 
       { 
          //There is no data. 
       } 

       } 

     } 

    } 

注:雖然這是沒有必要這個問題我提供此信息。我的活動有兩個佈局文件。一個是肖像和其他是爲風景模式在layout-land文件夾。

以下是logcat的窗口:

02-27 19:54:40.294: E/WindowManager(11710): Activity com.example.DataList has leaked window [email protected] that was originally added here 
02-27 19:54:40.294: E/WindowManager(11710): android.view.WindowLeaked: Activity com.example.prog.DataList has leaked window [email protected] that was originally added here 
02-27 19:54:40.294: E/WindowManager(11710):  at android.view.ViewRoot.<init>(ViewRoot.java:277) 
02-27 19:54:40.294: E/WindowManager(11710):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148) 
02-27 19:54:40.294: E/WindowManager(11710):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
02-27 19:54:40.294: E/WindowManager(11710):  at android.view.Window$LocalWindowManager.addView(Window.java:433) 
02-27 19:54:40.294: E/WindowManager(11710):  at android.app.Dialog.show(Dialog.java:265) 
02-27 19:54:40.294: E/WindowManager(11710):  at com.example.prog.DataList$LoadAsync.onPreExecute(DataList.java:77) 
02-27 19:54:40.294: E/WindowManager(11710):  at android.os.AsyncTask.execute(AsyncTask.java:391) 
02-27 19:54:40.294: E/WindowManager(11710):  at com.example.prog.DataList.onCreate(DataList.java:52) 
02-27 19:54:40.294: E/WindowManager(11710):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072) 
02-27 19:54:40.294: E/WindowManager(11710):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794) 
02-27 19:54:40.294: E/WindowManager(11710):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851) 
02-27 19:54:40.294: E/WindowManager(11710):  at android.app.ActivityThread.access$1500(ActivityThread.java:132) 
02-27 19:54:40.294: E/WindowManager(11710):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038) 
02-27 19:54:40.294: E/WindowManager(11710):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-27 19:54:40.294: E/WindowManager(11710):  at android.os.Looper.loop(Looper.java:150) 
02-27 19:54:40.294: E/WindowManager(11710):  at android.app.ActivityThread.main(ActivityThread.java:4277) 
02-27 19:54:40.294: E/WindowManager(11710):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-27 19:54:40.294: E/WindowManager(11710):  at java.lang.reflect.Method.invoke(Method.java:507) 
02-27 19:54:40.294: E/WindowManager(11710):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
02-27 19:54:40.294: E/WindowManager(11710):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
02-27 19:54:40.294: E/WindowManager(11710):  at dalvik.system.NativeStart.main(Native Method) 
02-27 19:54:40.294: E/ResourceType(11710): Style contains key with bad entry: 0x010102f3 
02-27 19:54:40.294: E/ResourceType(11710): Style contains key with bad entry: 0x01010300 
02-27 19:54:40.294: E/ResourceType(11710): Style contains key with bad entry: 0x0101039c 
+0

發佈所有logcat數據...然後我們可以瞭解什麼是問題或什麼異常時發生強制關閉發生。 – Ajit 2013-02-27 12:44:26

+0

一些代碼也可能有用。因爲當我有一個積極的ProgressDialog,我可以切換方向沒有任何麻煩。 – bofredo 2013-02-27 12:50:06

回答

3

在等待服務器的數據,您可以禁用方向變化。您可以設置方向縱向片刻:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

詳細,明確的解釋訪問:How do I disable orientation change on Android?

+0

即使在運行時,我也不想限制方向更改。此外,如果用戶從風景開始然後更改爲縱向,會發生什麼情況? – Apparatus 2013-02-27 13:23:26

+0

@Sam我想用戶通常在肖像模式下打開一個apk,但你是對的。但是也可以在開始從服務器下載數據之前強制用戶在縱向模式下啓動活動。如果您在職責之前設置了方向,則用戶無法將其更改爲風景,直到您再次讓他。而且,如果你願意,你可以檢查方向,然後設置橫向或縱向。請檢查訪問:http://stackoverflow.com/questions/4697631/android-screen-orientation – buttib 2013-02-27 13:43:22

+0

這是一個黑客,並有很多副作用。太多的開發人員會禁用方向更改作爲快速修復,以避免必須學習和編碼Activity生命週期。沒有這個,你會變得容易受到其他所有可能破壞和重新創建你的活動而不是方向變化的事情的影響。 – Simon 2013-02-27 14:13:49

2

你必須放棄你在onPause()對話框,如果需要的話,在onResume()再次顯示。

您可能想要使用onRetainNonConfigurationInstance()保留一些有關活動實例之間對話的狀態,並從傳遞給onCreate()的Bundle中檢索它。

+0

感謝您的時間和+1。我將用我的代碼編輯我的問題。你能詳細說明怎麼做嗎? – Apparatus 2013-02-28 08:12:14

+0

我用代碼更新了我的問題。請檢查。 – Apparatus 2013-02-28 09:12:15

0

這與Simon給你的答案是一樣的,但是有一個代碼示例。 您必須檢查進度是否完成,否則進度條會顯示卡住在100%。

@Override 
public void onPause() { 
    super.onPause(); // Always call the superclass method first 

    proDialog.dismiss(); 
} 

@Override 
public void onResume(){ 
    super.onResume(); 


    proDialog.show(); 
}