taken from http://developer.android.com/guide/topics/ui/settings.html:
Note: A PreferenceFragment doesn't have a its own Context object. If
you need a Context object, you can call getActivity(). However, be
careful to call getActivity() only when the fragment is attached to an
activity. When the fragment is not yet attached, or was detached
during the end of its lifecycle, getActivity() will return null.
If I call getActivity() from within the OnCreate() method of a PreferenceFragment then can I be assured that the fragment is attached to its activity - or is there some other way you should get the Context in this instance?
The reason I need a Context is I'm trying to use a Toast notification from the PreferenceFragment
If you want to make sure that a Context is available you'll need to wait until the Fragment has been attached to an Activity. The callback for this is the onAttach(Activity) method.
Depending on the lifecycle state of your fragment the getActivity() method can also return null.
Related
I am not asking for implementation or anything like that
I am very confused about how the fragment receives the context from Activity. I try to dig deep in Activity & Fragment inner working and all I can narrow it down is to FragmentHostCallback but I am unable to understand how it is working.
getActivity().getApplicationContext()
this way you can get applicationcontext() inside a fragment
androidx.fragment.app.Fragment has method getContext() that calls FragmentHostCallback host (Activity or Fragment to which is current Fragment attached) and returns Context of the host.
In the documentation of the android.support.v4.app.Fragment class (not the framework's class) for the getActivity() method stated that the returned value may be null if the Fragment is associated with a Context.
How an Fragment can be associated with a Context? Is not the FragmentManager the only way to attach an Fragment to something? But the FragmentManager can be obtained only from Activity. Or not?
How an Fragment can be associated with a Context?
You must have heard of FragmentHostCallback. If you haven't check out the link.
In a simple way, it is an integration point with a Fragment Host. When I say Fragment Host, It is an object that can hold Fragments. For example an Activity. In order to host a fragment - one must implement FragmentHostCallback.
However, I haven't come up with any ideas about how Fragment can be implemented in non-activity objects. Will see in future may be...
So that way, getActivity() will return null on non-activity objects.
PS,
Always go for getContext() if you are requiring context rather than activity
As I understand, you need the context inside of a fragment.. If so have you checked the method getContext() method inside of a fragment?
Also getActivity() can be null if you are referencing it when the fragment is not attached to an activity. Have a check of the fragment lifecycle to learn more.
Hope I helped
I know this is a basic question but i have seen that using the method getApplicationContext() to get the context work at places where the "this" keyword does't work, especially inside an onClickListener.
Why is this?
In the case of an OnClickListener, this is the anonymous class of the OnClickListener, therefore not a Context.
Whereas calling that method works because it's from the Activity class.
Alternatively, MyActivity.this works as well.
getActivity(): Used inside a Fragment to get the context of activity it is currently associated to.
this: Returns the context of current block in which it is called. If it is called inside an onClickListener then it would return the context of that listener, not the activity.
MyActivity.this: Returns the context of the activity. This can be used at the place of getActivity() as an alternate. (MyActivity should be read as the activity name you are using).
What is different between getContext() and getActivity() from Fragment in support library?
Do they always return the same object? (activity associated with current fragment)
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:
A null 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.
A non-null 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().
Activity is a subclass of Context. Activity has also Window elements and access to UI methods, Context doesn't. However, in the majority of the cases, it's the same if you need only the Context.
getContext():- Returns the context the view is currently running in. Usually the currently active Activity. getContext() is not defined in an Activity. It's used in a View (or View subclass) to get a reference to the enclosing context (an Activity).
getActivity():- This method gives the context of the Activity. You can use it is like the yourActivity.this. getActivity() is normally used in fragments to get the context of the activity in which they are inserted or inflated.
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 .
You can use getActivity(), which returns the activity associated with a fragment. The activity is a context (since Activity extends Context).
getActivity() can return null if it is called before onAttach of the respective fragment. Context provides information about the Actvity or Application to newly created components. Relevant Context should be provided to newly created components (whether application context or activity context). Since Activity is a subclass of Context, one can use this to get that activity's context.
getContext() Returns the context view only current running activity.
getContext() :
Returns the context view only current running activity.
getActivity():
Return the Activity this fragment is currently associated with.
/**
* Return the {#link Context} this fragment is currently associated with.
*/
public Context getContext() {
return mHost == null ? null : mHost.getContext();
}
/**
* Return the {#link FragmentActivity} this fragment is currently associated with.
* May return {#code null} if the fragment is associated with a {#link Context}
* instead.
*/
final public FragmentActivity getActivity() {
return mHost == null ? null : (FragmentActivity) mHost.getActivity();
}
from source code, we can find that when a fragment is attached to an Activity, getContext returns null. While getActivity returns null when a fragment is attached to context instead
I'm trying to change the activity title from a fragment (in this case, it's an android.support.v4.app.Fragment). To this end, I save the activity in an attribute on the fragment when onAttach() is called on the fragment. According to the docs, onAttach() should be called before onCreateView(), which I'm using to request some data used to fill up the view. When I kick off the thread for the network retrieval, I want to indicate that in the title, so I'm trying to call this.activity.setTitle() from the Fragment. However, that keeps throwing a NullPointerException. What am I missing here?
You can access the Activity in a Fragment using getActivity(). It can be called safely as soon as onActivityCreated() was called on the Fragment. Before that, it might not be there or might not have been fully initialized yet.
If your thread starts before that, just note the fact somewhere in your Fragment and only change the title after onActivityCreated was called.
Nowadays you can call requiredActivity() too that return FragmentActivityobject and if fragment doesn't come from an Activity, method throws a IllegalStateException