I receive this exception while change screen orientation in case showing progress dialog:
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)
I have tried many ways like AsyncTask does but they can't work. Here is one of them.
What I want to do is showing progress dialog when download data from fragment. Here is my try:
The fragment
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;
}
The activity
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;
}
}
I have no idea why this approach worked fine for asynctask but robospice
doesn't. Am I missing something?
P/S: set android:configChanges
in manifest or disable screen orientation maybe work but I don't want to use these approach. So, please tell me where I am wrong or give me a workable example with progress dialog using robospice
. Thanks in advance!
Update
As Snicolas recommend, I tried to use DialogFragments
, there is no exception, but when screen orientation changed while executing request, the dialog stop updating progress, it always showing and I can't dismiss it.
Your question is not related to RoboSpice per say. You should not use a Dialog, as it may leak its window... As it is in your case :)
It's quite difficult to do it well. That's why Google introduced DialogFragments. As you will see in the docs, DialogFragments are quite easy to use, you will basically wrap a dialog inside a Fragment.
This way, the FragmentManager will manage the life cycle of the dialog in pace with the life cycle of the activity.