Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crash at showing progress dialog #48

Open
AhmadullahSaikat opened this issue Aug 27, 2018 · 5 comments
Open

crash at showing progress dialog #48

AhmadullahSaikat opened this issue Aug 27, 2018 · 5 comments

Comments

@AhmadullahSaikat
Copy link

AhmadullahSaikat commented Aug 27, 2018

android.view.WindowLeaked: Activity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{360ddfca V.ED.... R......D 0,0-108,108} that was originally added here

code:

            hud = KProgressHUD.create(this)
                    .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                    .setCancellable(false)
                    .setAnimationSpeed(2)
                    .setDimAmount(0.5f)
                    .show();

@michzio
Copy link

michzio commented Sep 12, 2018

Yes I have the same issue it happens when there is Screen rotation. I have spinning progress then rotate, and in the same time there should be success KProgressHUD ... .show()

@michzio
Copy link

michzio commented Sep 12, 2018

Maybe here:

public KProgressHUD show() {
if (!isShowing()) {
mFinished = false;
if (mGraceTimeMs == 0) {
mProgressDialog.show();
} else {
mGraceTimer = new Handler();
mGraceTimer.postDelayed(new Runnable() {
@OverRide
public void run() {
if (mProgressDialog != null && !mFinished) {
mProgressDialog.show();
}
}
}, mGraceTimeMs);
}
}
return this;
}

Should be something like this

val activity = (context as? Activity)
if(activity != null && activity.window.decorView.isShown) {
//Show Your KProgressHUD

}

I tested outside as wrapper of hud.show() and it sometimes helps but not always as sometimes activity.window.decorView.isShown return true and as before detaching.

@michzio
Copy link

michzio commented Sep 12, 2018

I have modified KProgressHUD like this

public class KProgressHUD implements LifecycleObserver {

    private static final String TAG = "KProgressHUD";

    public enum Style {
        SPIN_INDETERMINATE, PIE_DETERMINATE, ANNULAR_DETERMINATE, BAR_DETERMINATE
    }

    // To avoid redundant APIs, make the HUD as a wrapper class around a Dialog
    private ProgressDialog mProgressDialog;
    private float mDimAmount;
    private int mWindowColor;
    private float mCornerRadius;
    private Context mContext;

    private int mAnimateSpeed;

    private int mMaxProgress;
    private boolean mIsAutoDismiss;

    private int mGraceTimeMs;
    private Handler mGraceTimer;
    private boolean mFinished;

    private Lifecycle mLifecycle;
    private boolean canShowProgressDialog = false;

    private Handler mMainUiHandler;

    public KProgressHUD(LifecycleOwner lifecycleOwner, Context context) {

        lifecycleOwner.getLifecycle().addObserver(this);
        mLifecycle = lifecycleOwner.getLifecycle();

        mContext = context;
        mMainUiHandler = new Handler(context.getMainLooper());
        mProgressDialog = new ProgressDialog(context);
        mDimAmount = 0;
        //noinspection deprecation
        mWindowColor = context.getResources().getColor(R.color.kprogresshud_default_color);
        mAnimateSpeed = 1;
        mCornerRadius = 10;
        mIsAutoDismiss = true;
        mGraceTimeMs = 0;
        mFinished = false;

        setStyle(Style.SPIN_INDETERMINATE);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    void onResume() {
        canShowProgressDialog = true;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    void onPause() {
        canShowProgressDialog = false;
        if(mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

and show() method

public KProgressHUD show() {
        if (!isShowing()) {
            mFinished = false;
            if (mGraceTimeMs == 0) {
                if(canShowProgressDialog) {
                    mMainUiHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            mProgressDialog.show();
                        }
                    });
                }
            } else {
                mGraceTimer = new Handler();
                mGraceTimer.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (mProgressDialog != null && !mFinished) {
                            if(canShowProgressDialog) {
                                mMainUiHandler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        mProgressDialog.show();
                                    }
                                });
                            }
                        }
                    }
                }, mGraceTimeMs);
            }
        }
        return this;
    }

now it seems to fix my issue while rotating screen and calling show(), and also non-UI thread issues.
But I need to call hud.show() after some delay ex. 500ms to correctly detect that Activity is destroying.

If you have any ideas please write them here.

@zhangchaoxu
Copy link

I encountered the same issue

@mikilangkilo
Copy link

try {
progressHUD.show();
}catch (Exception e){
CrashReport.postCatchedException(BuglyException.getInstance("kprogresshub error", e));
}

i have no idea why huawei mobile always crash at show(), i have no way but to add this.
please help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants