0

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

 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 一样的方法,但它们不起作用。是其中之一。我想要做的是从片段下载数据时显示进度对话框。这是我的尝试:
片段

 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/Sandroid:configChanges在清单中设置或禁用屏幕方向可能有效,但我不想使用这些方法。所以,请告诉我我错在哪里,或者给我一个使用进度对话框的可行示例robospice。提前致谢!
更新
正如Snicolas建议的那样,我尝试使用DialogFragments,没有例外,但是当执行请求时屏幕方向发生变化时,对话框停止更新进度,它总是显示并且我无法关闭它。

4

2 回答 2

3

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

把它做好是相当困难的。这就是 Google 引入 DialogFragments 的原因。正如您将在文档中看到的那样,DialogFragments 非常易于使用,您基本上将在 Fragment 中包装一个对话框。

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

于 2013-08-29T15:23:52.610 回答
-3

试试这个

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

在您的 Manifest.xml 中添加这些行

于 2013-08-25T04:26:57.660 回答