wajahatkarim3 / Today-I-Learned

📝 Today I Learned - A list of all things I learn on daily basis.

Home Page:https://hackernoon.com/today-i-learned-ok-technically-yesterday-65c64e1dcb6

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ReuireContext and RequireActivity description isn't accurate

humblerookie opened this issue · comments

Topic
https://github.com/wajahatkarim3/Today-I-Learned/blob/master/android/req-act.md

Section
Now, this method will make sure that Fragment is attached and returns a valid non-null Activity which we can use without any trouble.

This part isn't true in fact I'm not really sure of the purpose of these methods since they throw an IllegalStateException if your activity is detached rather than the nullable context unwrapping method.

Ref:

    public final Context requireContext() {
        Context context = getContext();
        if (context == null) {
            throw new IllegalStateException("Fragment " + this + " not attached to a context.");
        }
        return context;
    }

Yeah. I updated the article but on Medium and forgot to update it here. Thanks for pointing this. I have updated it here as well 😄