Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get application context from non activity singleton class

In my android project, I have ImageAdapter class in which I pass app context for some further needs.

public class ImageAdapter extends BaseAdapter {     private Context c;      public ImageAdapter(Context c) {             this.c = c;     }     ... } 

The problem is that I wanna make ImageAdapter as a singleton to have an easy access to the instance of this class from all of my activities. But I have no idea how to pass app context from getApplicationContext() method from one of my activities to ImageAdapter. So is there any "magic" to do that as follows?

public class ImageAdapter extends BaseAdapter {      private Context c;      private static class Holder {             public static final ImageAdapter IA = new ImageAdapter();     }      private ImageAdapter() {             this.c = /* some magic here */.getApplicationContext();     }      public static ImageAdapter getInstance() {             return Holder.IA;     }     ... } 

Maybe you have some other ideas for sharing ImageAdapter for any of my activities. I'm a newbie to android and I'm a little bit confused with the ways of passing data among activities.

I will be grateful for any help.

like image 792
Dmitry Avatar asked Feb 17 '14 00:02

Dmitry


People also ask

How do you find the activity context in an application class?

From B activity you create a object of class A using this constructor and passing getApplicationContext(). Show activity on this post. If you need the context of A in B, you need to pass it to B, and you can do that by passing the Activity A as parameter as others suggested.

What is the difference between Applicationcontext and activity context?

Application Context: It is the application and we are present in Application. For example - MyApplication(which extends Application class). It is an instance of MyApplication only. Activity Context: It is the activity and we are present in Activity.

What is an application class in Android?

Overview. The Application class in Android is the base class within an Android app that contains all other components such as activities and services. The Application class, or any subclass of the Application class, is instantiated before any other class when the process for your application/package is created.


1 Answers

Update: 06-Mar-18

Use MyApplication instance instead of Context instance. Application instance is a singleton context instance itself.

public class MyApplication extends Application {      private static MyApplication mContext;      @Override     public void onCreate() {         super.onCreate();         mContext = this;     }      public static MyApplication getContext() {         return mContext;     } } 

Previous Answer

You can get the the application context like this:

public class MyApplication extends Application {      private static Context mContext;      @Override     public void onCreate() {         super.onCreate();         mContext = getApplicationContext();     }      public static Context getContext() {         return mContext;     } } 

Then, you can call the application context from the method MyApplication.getContext()

Don't forget to declare the application in your manifest file:

<application     android:name=".MyApplication"     android:icon="@drawable/icon"     android:label="@string/app_name" > 
like image 184
mohammed momn Avatar answered Sep 23 '22 20:09

mohammed momn