android-context

How to pass context?

寵の児 提交于 2021-02-18 12:15:07
问题 I want to pass the context of the main activity to another class in order to create a Toast. My main activity calls a class that will delete a file. The class that deletes files will call a toast if the file does not exist. Here is my code: public class MyActivity extends AppCompatActivity { public void onCreate(Bundle savedInstanceState) { // create a file Button buttoncreate = (Button)findViewById(R.id.create_button); Button buttondelete = (Button)findViewById(R.id.delete_button); ...

Android getContext on a background Service

雨燕双飞 提交于 2021-01-02 06:50:31
问题 I'm trying to create a Service that runs even when my app is closed. However, I need to use my app Context inside this Service . When the app is running, the service works as well, but when I close the app (onDestroy() was called), the getContext() always returns null . Service public class SubscribeService extends Service { private Context context; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); context = this; //Returns

How to get Executor for main thread on API level < 28

自闭症网瘾萝莉.ら 提交于 2020-12-29 05:31:05
问题 On API level 28(Pie) a new method is introduced in the Context class to get Executor for the main thread getMainExecutor(). How to get this executor on API level below 28? 回答1: You can use (in activity for example): ContextCompat.getMainExecutor(this); https://developer.android.com/reference/androidx/core/content/ContextCompat.html#getMainExecutor(android.content.Context) 回答2: You can use code snippet from retrofit https://github.com/square/retrofit/blob/master/retrofit/src/main/java

Correct context to use within callbacks

十年热恋 提交于 2020-12-05 04:56:17
问题 The title pretty much says it all. If you have a callback from one class to another and need to call some method from within the callback that requires a context what is the correct context to use? A common example would be an AsyncTask with a callback to the Activity or Fragment that used it. I generally try to avoid using getApplicationContext() but I cannot use this as the context from within a callback. Is this a case where using a broader context is appropriate? To clarify further I'm

Correct context to use within callbacks

二次信任 提交于 2020-12-05 04:55:10
问题 The title pretty much says it all. If you have a callback from one class to another and need to call some method from within the callback that requires a context what is the correct context to use? A common example would be an AsyncTask with a callback to the Activity or Fragment that used it. I generally try to avoid using getApplicationContext() but I cannot use this as the context from within a callback. Is this a case where using a broader context is appropriate? To clarify further I'm

How is a context in Android created? What is the purpose of ContextThemeWrapper?

只愿长相守 提交于 2020-08-21 06:01:06
问题 I'm helping put together this page: What is a context? To help illustrate how components are related to a Context , I created this diagram from looking through the framework source code: After digging around a bit, I had the following questions: What creates a Context and what concrete classes are used? The base Context class itself is an abstract class, which requires almost all of its methods to be implemented. And a ContextWrapper , when instantiated, requires a Context instance to be

How to reference the current or main activity from another class

末鹿安然 提交于 2020-07-31 06:39:29
问题 I often find myself needing to access methods that require referencing some activity. For example, to use getWindowManager , I need to access some Activity. But often my code for using these methods is in some other class that has no reference to an activity. Up until now, I've either stored a reference to the main activity or passed the context of some activity to the class. Is there some better way to do this? 回答1: If you already have a valid context, just use this: Activity activity =

Preventing memory leaks in Android

亡梦爱人 提交于 2020-06-17 09:39:54
问题 Is it wise to get a reference to a Context object in every Activity where I need a Context by getting the Application context? I have learned that it can create memory leaks to throw around your Activity's context object but when you create complex Activities it seems that a Context object is almost always necessary. I was previously declaring a Context variable at the top of the Activity class and initializing it with the "this" keyword in onCreate . I already know this can be poor form, but

Preventing memory leaks in Android

梦想与她 提交于 2020-06-17 09:39:27
问题 Is it wise to get a reference to a Context object in every Activity where I need a Context by getting the Application context? I have learned that it can create memory leaks to throw around your Activity's context object but when you create complex Activities it seems that a Context object is almost always necessary. I was previously declaring a Context variable at the top of the Activity class and initializing it with the "this" keyword in onCreate . I already know this can be poor form, but