android-context

What is 'Context' on Android?

ぐ巨炮叔叔 提交于 2019-11-26 03:13:07
问题 In Android programming, what exactly is a Context class and what is it used for? I read about it on the developer site, but I am unable to understand it clearly. 回答1: Putting it simply: As the name suggests, it's the context of current state of the application/object. It lets newly-created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity and package/application). You can get the context by invoking

What's “tools:context” in Android layout files?

纵饮孤独 提交于 2019-11-26 02:59:44
问题 Starting with a recent new version of ADT, I\'ve noticed this new attribute on the layout XML files, for example: <LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\" xmlns:tools=\"http://schemas.android.com/tools\" android:orientation=\"vertical\" android:layout_width=\"fill_parent\" android:layout_height=\"fill_parent\" tools:context=\".MainActivity\" /> What is \"tools:context\" used for? How does it even know the exact path to the activity that is written there? Does

Using getResources() in non-activity class

那年仲夏 提交于 2019-11-26 02:51:39
问题 I am trying to use getResources method in a non-activity class. How do I get the reference to the \"resources\" object so that I can access the xml file stored under resources folder? Example: XmlPullParser xpp = getResources().getXml(R.xml.samplexml); 回答1: You will have to pass a context object to it. Either this if you have a reference to the class in an activty, or getApplicationContext() public class MyActivity extends Activity { public void onCreate(Bundle savedInstanceState) {

difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this

这一生的挚爱 提交于 2019-11-26 01:56:14
问题 I\'m new to android and I\'m trying to understand the difference between getApplication() , getApplicationContext( ), getBaseContext() , getContext() and someClass.this and especially when to use the these methods in the following code lines: When I launch a toast what is the difference between these and in which cases to I use them? Toast.makeText(LoginActivity.this, \"LogIn successful\", Toast.LENGTH_SHORT).show(); Toast.makeText(getApplication(), \"LogIn successful\", Toast.LENGTH_SHORT)

Dialog throwing "Unable to add window — token null is not for an application” with getApplication() as context

隐身守侯 提交于 2019-11-26 01:24:59
问题 My Activity is trying to create an AlertDialog which requires a Context as a parameter. This works as expected if I use: AlertDialog.Builder builder = new AlertDialog.Builder(this); However, I am leery of using \"this\" as a context due to the potential for memory leaks when Activity is destroyed and recreated even during something simple like a screen rotation. From a related post on the Android developer\'s blog: There are two easy ways to avoid context-related memory leaks. The most

How to get package name from anywhere?

杀马特。学长 韩版系。学妹 提交于 2019-11-26 00:57:39
问题 I am aware of the availability of Context.getApplicationContext() and View.getContext(), through which I can actually call Context.getPackageName() to retrieve the package name of an application. They work if I call from a method to which a View or an Activity object is available, but if I want to find the package name from a totally independent class with no View or Activity , is there a way to do that (directly or indirectly)? 回答1: An idea is to have a static variable in your main activity,

Calling startActivity() from outside of an Activity context

Deadly 提交于 2019-11-26 00:50:43
问题 I have implemented a ListView in my Android application. I bind to this ListView using a custom subclass of the ArrayAdapter class. Inside the overridden ArrayAdapter.getView(...) method, I assign an OnClickListener . In the onClick method of the OnClickListener , I want to launch a new activity. I get the exception: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? How can I get the Context that the ListView

Difference between Activity Context and Application Context

你。 提交于 2019-11-26 00:08:49
问题 This has me stumped, I was using this in Android 2.1-r8 SDK: ProgressDialog.show(getApplicationContext(), ....); and also in Toast t = Toast.makeText(getApplicationContext(),....); using getApplicationContext() crashes both ProgressDialog and Toast .... which lead me to this question: What is the actual differences between a activity context and application context, despite sharing the wording \'Context\'? 回答1: They are both instances of Context, but the application instance is tied to the

Calling startActivity() from outside of an Activity context

两盒软妹~` 提交于 2019-11-25 23:54:54
I have implemented a ListView in my Android application. I bind to this ListView using a custom subclass of the ArrayAdapter class. Inside the overridden ArrayAdapter.getView(...) method, I assign an OnClickListener . In the onClick method of the OnClickListener , I want to launch a new activity. I get the exception: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? How can I get the Context that the ListView (the current Activity ) is working under? Alex Volovoy Either cache the Context object via constructor in

getApplication() vs. getApplicationContext()

末鹿安然 提交于 2019-11-25 23:19:31
问题 I couldn\'t find a satisfying answer to this, so here we go: what\'s the deal with Activity/Service.getApplication() and Context.getApplicationContext() ? In our application, both return the same object. In an ActivityTestCase however, mocking the application will make getApplication() come back with the mock, but getApplicationContext will still return a different context instance (one injected by Android). Is that a bug? Is it on purpose? I don\'t even understand the difference in the first