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.
Related
When I checked lifecycle of fragment I found something that I do not understand.
FragmentHostCalback
Fragments may be hosted by any object; such as an Activity. In order to host fragments, implement FragmentHostCallback, overriding the methods applicable to the host.
Question
Where created FragmentHostCallback in Fragment?
I think FragmentHostCallback created at onInflate or onAttach. However, I couldn't find any code related to creating FragmentHostCallback there.
Fragments themselves don't host Fragments - as mentioned, it is an Activity or equivalent object that creates a FragmentHostCallback object. For example, FragmentActivity contains its own implementation of FragmentHostCallback called HostCallbacks.
You never interact with this object directly, rather, the FragmentHostCallback is passed to the FragmentController.createController(), which is what FragmentActivity, in this case, uses to trigger lifecycle changes and other signals from the Activity to the FragmentManager that you actually interact with.
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
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
It sounds me a confusing Question. What is the Activity Object in onAttach method of a fragment which is nested inside a parent Fragment?
I know I can communicate to the parent fragment by using getParentFragment() method.But I want to know if this activity object in the onAttach returns the FragmentActivity instance or not, so as to have a direct communication (by using interface callback) from a nested fragment to the FragmentActivity.
Sorry for my bad English. I tried my best to convey my thoughts.
Yes, the Activity object is your FragmentActivity. Check the docs here: http://developer.android.com/reference/android/app/Fragment.html#onAttach(android.app.Activity)
You can also get the activity by using getActivity() anywhere in your fragment (but be aware it only works after onActivityCreated has been called)
Yes it is the FragmentActivity that hosts the Fragment (directly or nested in another Fragment). There is more information about communicating with the parent FragmentActivity here.
I have a Fragment, which should execute a (public) Method reset() from the Activity in the Fragment it is called from.
I found, for example, this page:
Calling Activity methods from Fragment
The other way there's no problem:
Input_tap Input_tap = (Input_tap) getFragmentManager().findFragmentById(R.id.input);
Input_tap.reset();
Does someone has any idea how to do it the other way?
and: Does the Method has to be public - its a "void" Method?
A Fragment can call getActivity() to retrieve the activity that is hosting it.
If there is only one possible activity that can host the fragment, just cast the Activity to the proper class (e.g., MyActivity) and call a method on it:
((MyActivity)getActivity()).someMethod();
If there might be more than one activity that can host the fragment, you are best served by implementing a common interface on all those activities, so you can cast getActivity()'s result to that interface:
((MyInterface)getActivity()).someMethod();