What is the difference between Activity and Context?
As far as I understand:
Context is the Base Object. So every Activity same as Application derives from Context. This means that every Activity and every Application
IS a Context
;
From developer.android.com
Activity
java.lang.Object
↳ android.content.Context
↳ android.content.ContextWrapper
↳ android.view.ContextThemeWrapper
↳ android.app.Activity
And Application
java.lang.Object
↳ android.content.Context
↳ android.content.ContextWrapper
↳ android.app.Application
An Application context lasts, as long as your app is alive, while the Activity context dies with your Activity (it is not valid after onDestroy
of that Activity).
So if you need the Context across Activities (i.e. in a Singleton) you will be better off using an Application context.
Usually on Android Framework methods where a context is expected, it makes no difference which one you pass. But be always aware of MemoryLeaks if you're keeping long-living References to a Context
Difference between Activity Context and Application Context
They are both instances of Context, but the application instance is tied to the lifecycle of the application, while the Activity instance is tied to the lifecycle of an Activity. Thus, they have access to different information about the application environment.
If you read the docs at getApplicationContext it notes that you should only use this if you need a context whose lifecycle is separate from the current context. This doesn't apply in either of your examples.
The Activity context presumably has some information about the current activity that is necessary to complete those calls. If you show the exact error message, might be able to point to what exactly it needs.
But in general, use the activity context unless you have a good reason not to.
What is the difference between context and activity inside a fragment?
with context > intent exists as long as application exists
with activity > intent exists as long as the activity exists
now you know what you need most of the cases.. :-D
What is the difference between Context and Acitivity?
Context
: is a handle to the system. Context contains environment data such as local files, database, ... Context also include many system services. For example, Context will provide access to Location Service
,... Also, with context, you can use resources, access to databases and preferences, local data ...
Activity
: extends from Context
. And not only Activity
, many others extends Context
, and each has its own purpose such as: FragmentActivity
, Service
, WallpaperService
. More detail in Activity
, "normal" android app has activity. It's like a handle to the environment your application is currently running in. Activity
can create UI (User Interface)
In your above code, depends on other constructor, you should put context
or activity
object into. And as you see, if activity
takes from the same context
, you can use one parameter in the constructor
public AmazedView(Context context) {
Activity activity = (Activity) context;
}
But you can see, it's not clear, and "hide" real object behind. Because Activity
is the subclass of context, so in an informal way, Activity
has some "additional things" that context doesn't have. If you put it as a context object, no one knows that fact and will make the code seems obscure. Moreover, context might become from Service
for example, and you will don't sure when typecasting to Activity
. So, make two parameters is suitable here.
More importantly, you should carefully to use Activity as the context object because it might cause a memory leak.
activity context vs application context
If I'm using a one activity multiple fragments design for my app, would the application context in this case be the same as the activity context?
No. The Application
singleton is an entirely separate class.
I know it's a bad idea to pass the activity context to a ViewModel but in this case is it valid?
No — use the Application
.
The app is always in portrait
Note that there are many configuration changes, not just orientation. The user can switch locale, enable "dark mode" on Android 10+, enter or leave split-screen mode, resize a Chrome OS window, etc.
Difference- ApplicationContext() VS. Activity Context in ANDROID..?
To have a gist over the application context and activity context read below:
The application context will live as long as your application is alive and does not depend on the activities life cycle. If you plan on keeping long-lived objects that need a context, remember the application object. You can obtain it easily by calling Context.getApplicationContext() or Activity.getApplication().
In summary, to avoid context-related memory leaks, remember the following:
1.Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself)
2.Try using the context-application instead of a context-activity
Reference & more Info
What is different between MainActivity.this vs getApplicationContext()
Which context to use?
There are two types of Context:
Application context is associated with the application and will always be the same throughout the life of application; it does not change. So if you are using Toast
, you can use application context or even activity context (both) because Toast
can be displayed from anywhere within your application and is not attached to a specific window. But there are many exceptions. One such exception is when you need to use or pass the activity context.
Activity context is associated with the activity and can be destroyed if the activity is destroyed; there may be multiple activities (more than likely) with a single application. Sometimes you absolutely need the activity context handle. For example, should you launch a new Activity
, you need to use activity context in its Intent
so that the newly-launched activity is connected to the current activity in terms of activity stack. However, you may also use application's context to launch a new activity, but then you need to set flag Intent.FLAG_ACTIVITY_NEW_TASK
in intent to treat it as a new task.
Let's consider some cases:
MainActivity.this
refers to the MainActivity
context which extends Activity
class but the base class (Activity
) also extends Context
class, so it can be used to offer activity context.
getBaseContext()
offers activity context.
getApplication()
offers application context.
getApplicationContext()
also offers application context.
For more information please check this link.
Difference in Context for Activity and Fragment?
Activity extends Context, but Fragments do not. This is why you can pass it this
for a context parameter. So the error message sees your ScanFragment as a parameter for Context which does not work. Each Fragment is associated with an Activity's fragment. You can either use getActivity()
or getActivity().getApplicationContext()
to receive a valid context to use in these situations.
Your new code would be:mPreview = new CameraPreview(getActivity(), mCamera, previewCb, autoFocusCB);
Related Topics
Actionbar Logo Centered and Action Items on Sides
Is There a Simple Example of the Popupwindow Class Using Android V2.0
Notificationcompat with API 26
Differencebetween Activity and Context
Android Mediaplayer Throwing "Prepare Failed.: Status=0X1" on 2.1, Works on 2.2
How to Design Any Screen Size and - Density in Android(Multi Screen for Mobiles in Android)
Super.Oncreate(Savedinstancestate);
How to Start an Intent by Passing Some Parameters to It
Detect When Recyclerview Reaches the Bottom Most Position While Scrolling
Android.Database.Cursorindexoutofboundsexception: Index 0 Requested, with a Size of 0
Adb Is Not Recognized as Internal or External Command on Windows
Android Share Images from Assets Folder
How to Attach Multiple Files to Email Client in Android