Using Application context everywhere?

yanchenko

In an Android app, is there anything wrong with the following approach:

public class MyApp extends android.app.Application {

    private static MyApp instance;

    public MyApp() {
        instance = this;
    }

    public static Context getContext() {
        return instance;
    }

}

and pass it everywhere (e.g. SQLiteOpenHelper) where context is required (and not leaking of course)?

Reto Meier

There are a couple of potential problems with this approach, though in a lot of circumstances (such as your example) it will work well.

In particular you should be careful when dealing with anything that deals with the GUI that requires a Context. For example, if you pass the application Context into the LayoutInflater you will get an Exception. Generally speaking, your approach is excellent: it's good practice to use an Activity's Context within that Activity, and the Application Context when passing a context beyond the scope of an Activity to avoid memory leaks.

Also, as an alternative to your pattern you can use the shortcut of calling getApplicationContext() on a Context object (such as an Activity) to get the Application Context.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Evaluation context of @everywhere

Using application context in another module

Using @MockBean in tests forces reloading of Application Context

Application context returning null when using getFilesDir()

Using single spring application context for web app

Using mysql INTO OUTFILE in web application context

What is the (potential) drawback of using async functions everywhere?

How to get to application context path without using scriplets in JSP files?

Is there any way to load an application context using a custom BeanWrapper implementation

Application context is reloaded for every Spock specification when using EmbeddedKafka

Unable to pass context root to .war application in websphere using wsadmin

application context file get changed while building using pom

MVC Multitenant Application Instance Scope on Context using Autofac?

How to update state in Context using Hooks in MERN Stack Application

Microsoft App Registration - how to create meetings using OnlineMeetings.ReadWrite.All within an application context and not a user context?

Cannot initialize context because there is already a root application context present (using annotations only)

activity context vs application context

Loading context after application context

Passing `Context` everywhere seems messy - create classes to handle different interactions with context?

"Cannot resolve R" everywhere after converting module from application to feature

Spring Application - Application Context IOC

User context in rest application

Call Application context in Activity

Application context in Android

Getting Spring Application Context

application context. What is this?

Specify Custom Application Context

Spring application context encryption

Spring application context is null