处理robospice上的进度对话框和屏幕方向

时间:2013-08-25 04:14:59

标签: android android-fragments android-asynctask robospice

在显示进度对话框时更改屏幕方向时收到此异常:

 08-25 06:03:06.482: E/WindowManager(734): Activity com.org.scgroup.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43cf9838 that was originally added here
 08-25 06:03:06.482: E/WindowManager(734): android.view.WindowLeaked: Activity com.org.scgroup.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43cf9838 that was originally added here
 08-25 06:03:06.482: E/WindowManager(734):  at android.view.ViewRoot.<init>(ViewRoot.java:227)
 08-25 06:03:06.482: E/WindowManager(734):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
 08-25 06:03:06.482: E/WindowManager(734):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
 08-25 06:03:06.482: E/WindowManager(734):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
 08-25 06:03:06.482: E/WindowManager(734):  at android.app.Dialog.show(Dialog.java:239)
 08-25 06:03:06.482: E/WindowManager(734):  at com.org.scgroup.MainActivity.onPreExecute(MainActivity.java:253)
 08-25 06:03:06.482: E/WindowManager(734):  at com.org.scgroup.BookFragment.downloadBook(BookFragment.java:125)
 08-25 06:03:06.482: E/WindowManager(734):  at com.org.scgroup.BookFragment$1.onClick(BookFragment.java:255)
 08-25 06:03:06.482: E/WindowManager(734):  at android.view.View.performClick(View.java:2364)
 08-25 06:03:06.482: E/WindowManager(734):  at android.view.View.onTouchEvent(View.java:4179)

我尝试过像AsyncTask这样的很多方法,但它们无法工作。 Here就是其中之一。 我想要做的是从片段下载数据时显示进度对话框。这是我的尝试:
片段

 static interface TaskCallbacks {
    void onPreExecute();
    void onProgressUpdate(int percent);
    void onPostExecute();
    void onError();
    void reset();
}  
private class DownloadListener extends AbstractNotify<List<Entry>>{
    @Override
    public void onRequestFailure(SpiceException arg0) {
        Ln.e(arg0);
        isDownloading = false;
        mCallbacks.onError();
    }
    @Override
    public void onRequestProgressUpdate(RequestProgress progress) {
        mCallbacks.onProgressUpdate((int)progress.getProgress());
    }

    @Override
    public void onRequestSuccess(List<Entry> result) {
        Ln.d("total download %d files", result.size());
        isDownloading = false;
        mCallbacks.onPostExecute();
    }
}

 //Execute the download request
 getSpiceManager().excute(request, new DownloadListener());

 @Override
public void onAttach(android.app.Activity activity) {
    super.onAttach(activity);
    Ln.d("onAttach activity");
    mCallbacks = (TaskCallbacks) activity;

};

@Override
public void onDetach() {
    super.onDetach();
    mCallbacks = null;
} 

活动

    class BaseActivity implements TaskCallbacks{ 
    @Override   
   public void onCreate(Bundle savedInstanceState) {
                   // try to reshow dialog 
  if(null != savedInstanceState){
        isDownloading = savedInstanceState.getBoolean(IS_DOWNLOADING);
        currentProgress = savedInstanceState.getInt(CURRENT_PROGRESS);
        downloadDialog.setProgress(currentProgress);    
        if(isDownloading){
            downloadDialog.show();
        }

} 
 }

 @Override
public void onPreExecute() {
    if(null != downloadDialog){
        downloadDialog.show();
    }       
    isDownloading = true;
}
@Override
public void onProgressUpdate(int percent) {
    downloadDialog.incrementProgressBy(percent);

}
@Override
public void onPostExecute() {
    if(null != downloadDialog && downloadDialog.isShowing()){
        downloadDialog.setProgress(100);
        downloadDialog.dismiss();
    }

    isDownloading = false;
}
@Override
public void onError() {
    if(null != downloadDialog && downloadDialog.isShowing()){
        downloadDialog.dismiss();
    }

    isDownloading = false;  
}
}

我不知道为什么这种方法适用于asynctask但robospice没有。我错过了什么吗?
P / S :在清单中设置android:configChanges或禁用屏幕方向可能有效,但我不想使用这些方法。所以,请告诉我我错在哪里,或者使用robospice给我一个可行的示例,其中包含进度对话框。提前致谢!
更新
正如 Snicolas 推荐的那样,我尝试使用DialogFragments,没有例外,但是当执行请求时屏幕方向发生变化时,对话框停止更新进度,它始终显示,我无法解雇它

2 个答案:

答案 0 :(得分:3)

你的问题与RoboSpice无关。你不应该使用Dialog,因为它可能会泄漏它的窗口......就像你的情况一样:)

做得好很难。这就是谷歌推出DialogFragments的原因。正如您将在the docs中看到的,DialogFragments非常易于使用,您基本上将对话框包含在Fragment中。

这样,FragmentManager将按照活动的生命周期来管理对话框的生命周期。

答案 1 :(得分:-3)

试一下

        android:configChanges="orientation|screenSize|screenLayout|fontScale"

在Manifest.xml中添加这些行