Using Application Context Everywhere

Using Application context everywhere?

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.

How to get the Context from anywhere?

The easiest (and correct) way is:

Define a new class

public class MyApp extends Application {
private static MyApp instance;

public static MyApp getInstance() {
return instance;
}

public static Context getContext(){
return instance;
// or return instance.getApplicationContext();
}

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

Then in your manifest you need to add this class to the "Name" field at the "Application" tab. Or edit the xml and put

<application
android:name="com.example.app.MyApp"
android:icon="@drawable/icon"
android:label="@string/app_name"
.......
<activity
......

and then from anywhere you can call

MyApp.getContext();

Hope it helps

android get application context from anywhere but its always show class cast exeption i dont knoew how to use

Firstly, make sure you register your ApplicationContext in manifest file under android:name tag. Secondly, the problem that appears to be with your code is this line:

private ApplicationContext mAppContext = new ApplicationContext();

Here, you are basically creating a new instance of your application class. This is unnecessary because if your application class is registered in the manifest then it will automatically create an instance of this class. Moreover, I believe you want to do some like having reference of the context. Do something like this:

private Context mContext;

Then, in your newInstance get context as follows:

public static HomeFragment newInstance(Context context,
ActionListener actionListener) {
...
mContext = context;
...
}

You call this method as:

HomeFragment.newInstance(getApplicationContext(), listener);

Using application context in another module

Do your logger actually needs Context? No, it needs a File of external files dir.

fun initFileLog(sdCard: File) {
Log.e("FileLog", "create file")

if (initied) {
return
}
dateFormat = SimpleDateFormat("dd_MM_yyyy_HH_mm_ss", Locale.US)
try {
val dir = File(sdCard.absolutePath + "/logs")
...
}
}}

And you can provide such File from the app module.

As a rule of thumb, you should avoid Android dependencies in your modules, that enables them to be tested in simpler environment.

Access application context in companion object in kotlin

Actually I'm working inside an Android library and the class is abstract, so can't go with the already suggested solutions. However, I found way to do that.

  1. Creat a lateinit Context field inside companion object.
abstract class MyClass {

companion object {

private lateinit var context: Context

fun setContext(con: Context) {
context=con
}
}
}

  1. And then set it after the app has started
public class WelcomeActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);

MyClass.Companion.setContext(this);
}
}

Should passing the application context to so many classes in the application be avoided?

Read this blog by Romain Guy as in summary he said keeping activity context can cause memory leaks but if the context shared between classes is application context then the memory leak can be avoided.So sending application context will not cause you memory leak as per Romain Guy. :D

In my view and what i practice is, i pass context only if needed and context scope(lifecycle of Activity/Application) need's to be kept in mind before sending the context to different activity.



Related Topics



Leave a reply



Submit