Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between this, getContext() and getActivity()?

I am very confused with the usage of all these that where should we use them.

like image 536
Mr Singh Avatar asked May 16 '15 16:05

Mr Singh


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 the difference between getContext () getApplicationContext () getBaseContext () and this?

getApplicationContext() - Returns the context for all activities running in application. getBaseContext() - If you want to access Context from another context within application you can access. getContext() - Returns the context view only current running activity.

What is the difference between this context and getApplicationContext which one to use when?

This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component. Actually in general it is better to use getApplicationContext() since it will less likey lead to memory leaks.

What does getContext return in Android?

getContext(): It returns the Context which is linked to the Activity from which it is called. This is useful when we want to call the Context from only the current running activity.


2 Answers

this - return self reference
getContext() - return Context
getActivity() - return Activity

Context.

Quote from original answer :

As the name suggests, its the context of current state of the application/object. It lets newly created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity, package/application)

Activity

Activity is a Java code that supports a screen or UI. In other words, building block of the user interface is the activity. Activity class is a pre-defined class in Android and every application which has UI must inherit it to create window. Activity represents the presentation layer of an Android application, e.g. a screen which the user sees. An Android application can have several activities and it can be switched between them during runtime of the application.

Note : Activity extends Context. Context not an Activity.

like image 55
Sergey Shustikov Avatar answered Sep 24 '22 21:09

Sergey Shustikov


Activity is a subclass of Context, so whenever a context is required, either can be given.

getActivity() is at least a method on Fragment, to get the activity it is attached to.

Whenever a context is needed in an instance method of an activity, you can use this.

A context is needed whenever contextual info is needed, or when stuff needs to be displayed.

like image 23
wvdz Avatar answered Sep 21 '22 21:09

wvdz