What is different between getContext()
and getActivity()
from Fragment
in support library?
Do they always return the same object? (activity associated with current fragment)
getContext() - Returns the context view only current running activity. getActivity()- Return the Activity this fragment is currently associated with. getActivity() can be used in a Fragment for getting the parent Activity of the Fragment .
The method getActivity() is normally used in fragments to get the context of the activity in which they are inserted or inflated. getApplicationContext() Returns the context for the entire application (the process all the Activities are running inside of).
getActivity() in a Fragment returns the Activity the Fragment is currently associated with. (see http://developer.android.com/reference/android/app/Fragment.html#getActivity()).
If it's invoked when your fragment is not attached to activity, getContext() will return null. The ideal solution here is to Cancel the request when the activity/fragment is not active (like user pressed back button, or minimized the app).
When we use Fragment in our app, we often time need access to Context or Activity. We do it by calling methods such as getContext () and getActivity () methods.
The reason why getActivity () in Fragment is not recommended is as follows: This method will return the Activity attached to the current Fragment. When the Fragment life cycle ends and is destroyed, getActivity () returns null, so one needs to handle the null cases which might arise while using getActivity () .
When we use Fragment in our app, we often time need access to Context or Activity. We do it by calling methods such as getContext () and getActivity () methods. But, in kotlin, these methods return nullables and we end up using code like this.
Activity is the UI of an application through which user can interact and Fragment is the part of the Activity, it is a sub-Activity inside activity which has its own Life Cycle which runs parallel to the Activities Life Cycle. To read more refer to Activity Lifecycle in Android with Demo App.
In most cases there is no difference but ...
So originally Fragments
were hosted in FragmentsActivity
and back then to get Context
one called getActivity()
.
Just checked the sources and Fragments
now can be hosted by anyone implementing FragmentHostCallback interface. And this changed in Support Library version 23, I think.
When using newer version of Support Library, when Fragment
is not hosted by an Activity
you can get different objects when calling getActivity()
and getContext()
.
When you call getActivity()
you get an Activity
which is a Context
as well. But when you call getContext
you will get a Context
which might not be an Activity
.
So far, the only provided implementation of FragmentHostCallback
(in the OS and the support library) always returns the same value for both getContext()
and getActivity()
.
However, the other constructors of FragmentHostCallback
suggest that in future implementations, we may get:
Activity
and a non-null Context
which is not an Activity
. This looks improbable but we can imagine that fragments could be used outside Activities in the future, or be fully sandboxed.Activity
and a non-null Context
which is not the same instance as the Activity
. For example, Context
could be a ContextThemeWrapper
.Conclusion: when you can, use getContext()
. When you need Activity-specific calls, use getActivity()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With