android-context

What's the difference between the various methods to get a Context?

好久不见. 提交于 2019-11-25 22:37:12
问题 In various bits of Android code I\'ve seen: public class MyActivity extends Activity { public void method() { mContext = this; // since Activity extends Context mContext = getApplicationContext(); mContext = getBaseContext(); } } However I can\'t find any decent explanation of which is preferable, and under what circumstances which should be used. Pointers to documentation on this, and guidance about what might break if the wrong one is chosen, would be much appreciated. 回答1: I agree that

When to call activity context OR application context?

痞子三分冷 提交于 2019-11-25 22:16:05
问题 There has been a lot of posting about what these two contexts are.. But I\'m still not getting it quite right As I understand it so far: Each is an instance of its class which means that some programmers recommend you to use this.getApplicationContext() as often as possible in order to not \"leak\" out any memory. This is because the other this (getting the Activity instance context) points to an Activity that is being destroyed each time the user tilts the phone or leave the app etc.. Which

Difference between getContext() , getApplicationContext() , getBaseContext() and “this”

半世苍凉 提交于 2019-11-25 22:03:52
问题 What is the difference between getContext() , getApplicationContext() , getBaseContext() , and \" this \"? Though this is simple question I am unable to understand the basic difference between them. Please give some easy examples if possible. 回答1: View.getContext(): Returns the context the view is currently running in. Usually the currently active Activity. Activity.getApplicationContext(): Returns the context for the entire application (the process all the Activities are running inside of).

Using Application context everywhere?

♀尐吖头ヾ 提交于 2019-11-25 22:00:17
问题 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)? 回答1: 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

Using context in a fragment

a 夏天 提交于 2019-11-25 21:57:34
问题 How can I get the context in a fragment? I need to use my database whose constructor takes in the context, but getApplicationContext() and FragmentClass.this don\'t work so what can I do? Database constructor public Database(Context ctx) { this.context = ctx; DBHelper = new DatabaseHelper(context); } 回答1: You can use getActivity(), which returns the activity associated with a fragment . The activity is a context (since Activity extends Context ) . 回答2: To do as the answer above, you can

Static way to get 'Context' in Android?

天涯浪子 提交于 2019-11-25 21:45:11
问题 Is there a way to get the current Context instance inside a static method? I\'m looking for that way because I hate saving the \'Context\' instance each time it changes. 回答1: Do this: In the Android Manifest file, declare the following. <application android:name="com.xyz.MyApplication"> </application> Then write the class: public class MyApplication extends Application { private static Context context; public void onCreate() { super.onCreate(); MyApplication.context = getApplicationContext();