Get application context from non activity singleton class

跟風遠走 提交于 2019-11-26 06:38:56

问题


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.


回答1:


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" >



回答2:


I'd rather pass a context instance as a parameter to every method in singleton which really needs it




回答3:


APPROACH #1:

Since you specify that ImageAdapter is a singleton, one simple answer is to create that singleton from a class that has access to app context:

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ImageAdapter.createIt(this);
    }
}

public class ImageAdapter extends BaseAdapter {
    private static ImageAdapter it;
    // Get the singleton.
    public static ImageAdapter getIt() {
        return it;
    }
    // Call this once, to create the singleton.
    public static void createIt(Context context) {
        it = new ImageAdapter(context);
    }

    private final Context c;
    private ImageAdapter(Context context) {
        c = context;
    }
}

APPROACH #2:

If it were not a singleton, then I would use the accepted answer. In that case, remove the local variable from ImageAdapter, because context can always be obtained from MyApplication. Expanding on the accepted answer, if you want a local method as a convenience, define ImageAdapter.getContext(). Complete solution:

public class MyApplication extends Application {
    private static Context appContext;
    public static Context getContext() {
        return appContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        appContext = this;
    }
}

public class ImageAdapter extends BaseAdapter {
    public ImageAdapter() {
    }

    // [Optional] Call this whenever you want the app context.
    private Context getContext() {
        return MyApplication.getContext();
    }
}


来源:https://stackoverflow.com/questions/21818905/get-application-context-from-non-activity-singleton-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!