Application vs Activity Context in Android - android

I am noob in Android. I see there are many methods associated to get context either activity or application context. What context you should use in what scenario ??
One blog saying to use the context-application instead of a context-activity to avoid memory leaks. How here memory can be leaked if I use Activity context
Any idea ???

You create one Method that returns Context(this). Parse Activity to Application as below (under Methods defined in MainActivity class):
public Context getContext() {
return getApplicationContext();
}
public Activity getActivity() {
return this;
}
and then use:
Application abc = ((Application)getActivity());

Related

Why are we using Context in creating object for other class in Android?

In some places we were giving like "DatabaseUtil db=new DatabaseUtil(DailyPlanView.this);" where DatabaseUtil is the class with the constructor argument is context. But if we create the object for the DatabaseUtil class in the DailyPlanVIew class we are using the above code. My doubt is what is the use of the context and instead of passing the context object as argument why we are passing "this".
Whenever you are dealing with Context, its important to understand its used in everything. From using a database to obtaining system services. This is required by the way android works with Context. Specifically when you are passing this you are basically passing the class that encapsulates this statement.
class MyActivity extends Activity
{
onCreate(Bundle bundle)
{
View v = new View(this);
}
}
passing this refers to the object that encapsulates it. This is a Object oriented concept... Where this is reffering to MyActivity. One thing to keep in mind when passing context is ensure that you are passing the correct kind. Some Context objects have a longer lifespan than others and if not managed properly can lead to Context leaking. Specifically in this example, this works because Activity extends Context.
The differences occur in the View class.
getApplicationContext()
getBaseContext()
this, which in the Context of an activity has the life span of an Activity (Example above)
One thing to add about Context is that it is basically a reference to the current Application and it's specific data.
Some more information about context can be found in this thread:
What is 'Context' on Android?

Access context from anywhere? Global context?

I have an application which need to access context in a lot of different classes all the time, for saving and serializing data, showing dialogs etc.
According to an article on the Android developer site, this causes memory leaks:
http://developer.android.com/resources/articles/avoiding-memory-leaks.html
What is the general approach for accessing context? Should a create a singelton class which holds one reference to context as soon as the app is started or what's the best approach?
Right now my methods look like this for instance
public void saveData(TheCassName classObject, Context context){
//do some stuff that involves context
}
And is called from wherever i need it.
Thanks!
Just to clear: There is no memory leak as context which is getting saved, is part of the application which is a process which will only get killed when the application will close.
Extend application in your app and then use the application context by
making static variable in that.
public class MyApp extends Application {
private static Context context;
public void onCreate() {
super.onCreate();
MyApp.context = getApplicationContext();
}
public static Context getAppContext() {
return MyApp.context;
}
}
You need to define application also in your manifest too.
<manifest>
<application android:name="com.abc.MyApp">
</application>
</manifest>
Try using application context instead of activity context. However, there are limitations on app context you should be aware of: When to call activity context OR application context?

Getting the Application Context

This might be a simple question but I just wanted to make sure I am right.
In my android application I have a constructor that uses:
activity.getApplicationContext()
The activity is passed into the constructor as a parameter.
The problem is that I am calling this class from a Service. If I make a second constructor which accepts the Service as a parameter and uses service.getApplicationContext? Will I get the same application context?
The easiest way to get the application context is:
Create a class App that extends android.app.Application
public class App extends Application {
public static Context context;
#Override public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
}
Modify your AndroidManifest.xml 's <application> tag to have the attribute android:name="your.package.name.App".
Any time you need the application context, just get it from App.context.
Application is always initialized first whether your process runs, whether it's an activity, a service, or something else. You will always have access to the application context.
Will I get the same application context?
Yes. You can check the android documentation, they have provided
getApplicationContext()
Return the context of the single, global Application object of the current process.
So it should not be changed for the whole application process.
Please also take a note of this:
getApplicationContext() 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.
Correct me if I'm wrong.
Thanks
There is only one application context, so you should get the same one. You can have just one constructor that takes a Context, you don't really need two. Or if you wanted to make sure that you are getting the application context, and not, say, an activity one, you can have your constructor take Application as a parameter which is a Context.
You can go for getApplicationContext() if you wanna get context of whole application. If you want to get context of current class you can use getBaseContext() instead.
I have adapted yuku's answer with a non static direct context reference.
Create a class domain.company.pseudo.ApplicationName which extends android.app.Application.
package hypersoft.systems.android;
import android.app.Application;
public class Starbox extends Application {
public static Starbox instance;
#Override
public void onCreate() {
super.onCreate();
instance = this;
}
}
In this sample, my full application package name is hypersoft.systems.android.starbox.
Now, modify your AndroidManifest.xml <application> tag to have the attribute android:name="hypersoft.systems.android.Starbox", and be sure the Starbox.java class file is located in the project component directory: android rather than starbox.
With all this done, you can now import hypersoft.systems.android.Starbox, and in your code you can get the ApplicationContext by calling Starbox.instance.getApplicationContext()
Successfully compiling with build tools 26 and api 26 (Android 8.0) with min sdk version 14 (4.0).
Application Context add Activity Context both are different.Downcasting is risky .Use this code to use context object .
public class App extends Application {
public static Context context;
#Override public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
}
In Your Activities and in fragments Class :
Conetext context=App.context;

Using objects obtained through android Context after the context is invalid

I have been working with android for a little while now and feel pretty comfortable with the platform, but I have gotten a little confused with the Lifecycle of Context Objects. Looking at the hierarchy it is easy to see that Activity and Service both extend Context, and while this is convenient, it is concerning. I have avoided making helper classes that need a shared resource have a static field holding a context (since just about all resources come through some interaction with a Context object) so that way when an activity is destroyed, the GC is free to free it at any time, but I am wondering about resources fetched from a Context.
For example, if I have a static field that holds a File inside of a class. Then make this class's constructor take the current context and assign the File a File resource fetched through the Context passed in, the do nothing else with the Context in my 2ndary class, am I still holding on in some way to the Context?
class testClass{
private static File someFile;
public testClass(Context context){
synchronized(testClass.class){
if(someFile!=null){
//even though I am holding a File, or a SharedPreference Object generated from this context, am I correctly preventing this utility class from holding the Activity object in memory for no reason?
someFile = context.openFileOutput("Some_File.txt", Context.MODE_PRIVATE);
}
}
}
}
I did just read about Context.getApplicationContext() (Sadly not static). It says it returns a context relative to the process and not the activity so if I need to keep a context around, use that one. But the question above still remains.
I remembered I asked this question and thought I would answer it.
Though there may be more kinds of contexts, the primary ones developers use are the Activity Context, and the Application Context (and other things like Service Context). The Activity context is created and destroyed with the activity, so it is not a good idea to use as a constant reference stored between activity creation and destruction. The Application Context doesn't have some of the things an Activity Context has, but everything you would want a static context reference for is there (file IO, preferences...). The application context is also created and destroyed with the application, so you can guarantee that as long as your application code is running, the context is valid.
Because of this, the Application context should be used for things like worker threads that may need a constant access point to a context but not need access to an activity. The best way I have learned to do this is to extend the android Application class. This class is created when the application is created in memory, and as soon as the Application onCreate method is called, the Application Context is valid. This means you can create a static function in your custom application class that gives access to the context.
public class CustomApplication extends Application {
private static Context context;
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
public Context getAppContext() {
return context;
};
}
The only other thing you need to make this work is a modification to your manifest file so android knows to use your application class instead of the default.
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:name=".CustomApplication" >

Needing Context in non-Activity classes

I have some classes within my application that need to call Android functions that require the Context as a parameter.
I don't have it as the class is not a subclass of the Activity class.
What is the correct way to tackle this problem?
Pass it as a parameter on each call?
Pass it at class instantiation and keep it?
It depends on the role of the class. But anyway pass ApplicationContext but not Activity one. If you pass Activity context gc can't remove it from the memory when after you don't need activity anymore. But application context is used while application was not finished by OS.Refer Avoid Memory Leaks
Pass it as a parameter. Or better yet, get the application context to avoid memory leaks.
public class Example {
protected Context context;
public Example(Context context){
this.context = context.getApplicationContext();
}
}
I'm pretty much always going with a constructor parameter approach. I pass it in the instantiation and keep a private reference in the instantiated class.
You have to think about one important thing. If the class you pass the Context will exist longer than the Activity instantiating it then you should use the application context. If that class is doing UI stuff you will need an activity context.
Make sure that the class you are passing an activity context to won't last longer than the Activity or you'll leak the entire activity.
If you don't do UI stuff then go with the application context.
I pass it as a parameter, i think its de best form to do it
Pass it at class instantiation and keep it.
One typical example is when you create a db helper. See this link
I've answered this question here also.
You can do that using ContextWrapper, as described here.
For example:
public class MyContextWrapper extends ContextWrapper {
public MyContextWrapper(Context base) {
super(base);
}
}
and use that class as it were Context
The best way is to follow Bean approach:
public class Example {
protected Context getContext() {
...
}
...
}
Then it depends on possibilities to access context. If class is fully independent then constructor parameter and private field seems best approach.
But that bean property way shields you from further code changes.

Categories

Resources