Search code examples
androidandroid-layoutandroid-viewandroid-windowmanager

Difference between getWindow().setBackgroundDrawable and getWindow().getDecorView().setBackgroundDrawable


What is the difference between them? As far as I can see they both do the same thing.


Solution

  • If you go to sources, you will find PhoneWindow.class that have implementation of Window.setBackgroundDrawable method:

    @Override
    public final void setBackgroundDrawable(Drawable drawable) {
        if (drawable != mBackgroundDrawable || mBackgroundResource != 0) {
            mBackgroundResource = 0;
            mBackgroundDrawable = drawable;
            if (mDecor != null) {
                mDecor.setWindowBackground(drawable);
            }
            if (mBackgroundFallbackResource != 0) {
                mDecor.setBackgroundFallback(drawable != null ? 0 : mBackgroundFallbackResource);
            }
        }
    }
    

    As you can see, it calls different method of DecorView setWindowBackground:

    public void setWindowBackground(Drawable drawable) {
        if (getBackground() != drawable) {
            setBackgroundDrawable(drawable);
            if (drawable != null) {
                drawable.getPadding(mBackgroundPadding);
            } else {
                mBackgroundPadding.setEmpty();
            }
            drawableChanged();
        }
    }
    

    Which is actually use method DecorView.setBackgroundDrawable but also provides additional logic for Window. So I would suggest you to use getWindow().setBackgroundDrawable().