Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is different between getContext and getActivity from Fragment in support library?

Tags:

android

What is different between getContext() and getActivity() from Fragment in support library?

Do they always return the same object? (activity associated with current fragment)

like image 339
mlevytskiy Avatar asked Aug 26 '15 12:08

mlevytskiy


People also ask

What is the difference between getActivity and getContext?

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 .

What is getActivity in 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).

What is getActivity in Android?

getActivity() in a Fragment returns the Activity the Fragment is currently associated with. (see http://developer.android.com/reference/android/app/Fragment.html#getActivity()).

Can getContext return null?

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).

How to get activity and context from a fragment?

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.

Why can’t I use getactivity () in fragments?

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 () .

How to get activity and context in Kotlin fragments?

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.

What is the difference between activity and fragment in Android?

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.


2 Answers

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.

like image 197
pelotasplus Avatar answered Sep 26 '22 19:09

pelotasplus


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().

like image 41
BladeCoder Avatar answered Sep 25 '22 19:09

BladeCoder