Questions tagged android-context

Explore the latest questions and answers asked by our top developers.

Static Context: Is it worth it? – Android

I have this code in my Application class: public class MyApp extends MultiDexApplication { private static Application context; @Override public void onCreate(){ super.onCreate(); context = this; } @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); } public static Context getAppContext() { return context.getApplicationContext(); } } When I want to access the context I use this code: […]

By Student
Published
Categorized as android, android-context, android-studio, memory-leaks, performance

Static Context: Is it worth it?

I have this code in my Application class: public class MyApp extends MultiDexApplication { private static Application context; @Override public void onCreate(){ super.onCreate(); context = this; } @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); } public static Context getAppContext() { return context.getApplicationContext(); } } When I want to access the context I use this code: […]

By Student
Published
Categorized as android, android-context, memory-leaks, performance

is this good way to access getContext() | requireContext() method each time in Fragment?

I generally prefer to use one Context object and assign value by using getContext() or requireContext() method only once while initializing object inside any Fragment. I just wanted to know that is this right approach and it utilize less resource for better coding? Another approach is to use getContext() or requireContext() each time while required […]

What to replace "app context" with in Android Studio?

Someone recommended me this solution to another question I asked (How can I make a button that removes contacts once someone clicks on them, in Android?), which was this one: how to delete all contacts in contact list on android mobile programatically. In this question, the best answer shows this code: ContentResolver contentResolver = <your […]

By Andr Almeida
Published
Categorized as android, android-context

Fragment not attached to a context – Delayed UI changes

I am working with Fragments, and in a specific Fragment, I need to change the UI after a delay. I have found some code online which allows me to do so (using a Timer + CoroutineScope to touch the views). timer.schedule(object : TimerTask() { override fun run() { MainScope().launch { withContext(Dispatchers.Default) { } changeUI(requireContext()) } […]

how do I inject MainActivity context using dagger2 in java?

I am trying to inject the context of the MainActivity into a class I have. I looked at this but it doesn’t work for me and I get the following error: /home/muhammadmehdi/ALL_PROJECTS/memex/memex-app/app/src/main/java/com/memex/MainActivity.java:30: error: cannot find symbol import com.memex.components.DaggerMainActivityComponent; ^ symbol: class DaggerMainActivityComponent location: package com.memex.component This is the code that I wrote in the onCreate […]

By Sarim Mehdi
Published
Categorized as android, android-context, dagger-2, java

requestLocationUpdates never returns nor crashes

I’m absolutelly newbie with Android and Java. I’m trying to obtain a gps location listener. It works from an activity but not from my thread. My thread uses my own looping system. The very strange thing to me is than requestLocationUpdates seems to never come back from its execution nor crashes. I can read in […]

By Greenlight
Published
Categorized as android, android-context, android-gps, multithreading

Why does the doc do not use `this` i.e `Service` for the argument for `Context` in the documentation?

What is the usefulness of having Service inherit from Context if it does not use it? For example from the android docs for sync adapter we see: public class SyncService extends Service { // Storage for an instance of the sync adapter private static SyncAdapter sSyncAdapter = null; // etc sSyncAdapter = new SyncAdapter(getApplicationContext(), true); […]

By Jim
Published
Categorized as android, android-context, android-service, android-syncadapter

App crashing for some users due to kotlin.UninitializedPropertyAccessException

I’ve created a sharedPreference object which is being initialized in Application class. Here’s is the SharedPreference Object: object PreferenceUtil { … private lateinit var mPref: SharedPreferences fun init(context: Context) { if (!this::mPref.isInitialized) mPref = PreferenceManager.getDefaultSharedPreferences(context.applicationContext) } } Also, my application class: class MyApplication : Application() { override fun onCreate() { super.onCreate() PreferenceUtil.init(this) } } And […]

By Jazib Khan
Published
Categorized as android, android-context, kotlin, sharedpreferences

What is the context passed to a broadcast receiver and contentprovider?

Activity and Service inherit from Context while BroadcastReceiver and ContentProvider do not. The BroadcastReceiver gets a Content in the onReceive() method while in the ContentProvider a Context is available via the getContext(). My question is, what is the source of these contexts? How are they created? I mean when our code runs in an activity […]

By Jim
Published
Categorized as android, android-contentprovider, android-context, broadcastreceiver
1 2 3 11

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq