android-context

Android Asyntask: Use weak reference for context to avoid device rotate screen

依然范特西╮ 提交于 2019-11-27 10:17:57
问题 In Apress Pro Android 4 the author has said that: [...] context of currently running activity will no longer be valid when the device is rotated. [...] One approach is to use a weak reference to the activity instead of a hard reference [...] But the author just suggest this, and does not tell how it is done. Who has done this before please give me an example. 回答1: Somewhere in your AsyncTask you'll want to pass in your activity. Then you'll save that reference in a weak reference. Then you

Difference in context this and getContext()

狂风中的少年 提交于 2019-11-27 10:17:51
问题 What is difference between this and getContext() , when I say this I mean this within an Activity . 回答1: In general there are two type of classes. Ones that extend ContextWrapper class ( Activity , Service , Application ) and those that do not extend it (like View ). If class extends ContextWrapper then you can use this as Context . Such classes normally do not have getContext() method. Those classes that do not extend ContextWrapper but still save and use Context normally expose getContext()

Close the current activity when you only have a reference to Context

一曲冷凌霜 提交于 2019-11-27 10:07:50
问题 If I have a reference to Context , is it possible to finish the current activity? I don't have the reference to current activity. 回答1: yes, with a cast: ((Activity) ctx).finish(); 回答2: I know it's an old post but, perhaps it could be a good idea to call it this way: if(context instanceof Activity){ ((Activity)context).finish(); } This way we make sure we don't get any unnecesary ClassCastExceptions 回答3: In my Case following worked, I need to finish my activity in a AsyncTask onPostExcute().

getString Outside of a Context or Activity

此生再无相见时 提交于 2019-11-27 10:06:55
I've found the R.string pretty awesome for keeping hardcoded strings out of my code, and I'd like to keep using it in a utility class that works with models in my application to generate output. For instance, in this case I am generating an email from a model outside of the activity. Is it possible to use getString outside a Context or Activity ? I suppose I could pass in the current activity, but it seems unnecessary. Please correct me if I'm wrong! Edit: Can we access the resources without using Context ? Gangnus Yes, we can access resources without using `Context` You can use: Resources

How to retrieve a context from a non-activity class?

六眼飞鱼酱① 提交于 2019-11-27 09:26:38
I have found one answer that appears to say I should create a separate class and make a static MyApplication object and make a get method. Then any class can call MyApplication.get() to retrieve the context. Is there any other cleaner way? This is my situation: I have a class A and a class B. Class A contains an object from class B (let's call the object b). In class A I call, "b.play()". However, I get a null pointer exception because class B needs to pass a context to the MediaPlayer.create() method. Until now I threw together a hack and from class A I called.... "b.play(this)" and simply

android - How to get view from context?

冷暖自知 提交于 2019-11-27 07:32:34
I want to get the view or findViewById() from Context? Or from intent? I'm trying to reach a specific view in my broadcast receiver and the parameter of onReceive are context and intent. Well, I have a class and within it is my broadcast receiver. Now, I'm trying to separate the broadcast receiver from it, but I need a way so I can still communicate with the views on my class from my separated broadcast receiver class. Thanks. For example you can find any textView: TextView textView = (TextView) ((Activity) context).findViewById(R.id.textView1); Starting with a context, the root view of the

Android Get Application's 'Home' Data Directory

孤者浪人 提交于 2019-11-27 06:40:00
A simple question, relating to the default 'home' directory when an app writes to the internal memory. By default, any files created are placed by the OS (2.2) in: /data/data/your.package/files When reading in files, the same default is used, when keeping in proper context via openFileInput() , openFileOutput() . But if I need to check file existence, for instance, using the File class, I need to specify the whole path in the constructor. I see there are Environment.getDataDirectory() (returns /data ), Environment.getRootDirectory() (returns /system ), etc, but nothing related to getting the

Use of Context to start another Activity

梦想的初衷 提交于 2019-11-27 05:01:45
To start an Activity you need an Intent, like: Intent i = new Intent(context, class) So to fill in the context parameter, a couple of options are available: Use MyActivity.this or just this Use getApplicationContext() Use getBaseContext() And I'm sure there are one or two more options. These options all appear in some sort of tutorial, one uses the first, the next uses the third option. So which one should I use? Does it even matter? Is it different for different cases? Lalit Poptani Yes its different for different cases, It depends on the scope. Suppose if you are creating a method in a

How to Correctly Extend LinearLayout to Create a Custom View

廉价感情. 提交于 2019-11-27 04:33:16
问题 I have some "card", which is a simple LinearLayout with a TextView inside <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/card_label_txt" android:layout_width="wrap_content" android:text="label" /> </LinearLayout> then I have my Main Fragment with a vertical LinearLayout.. and in this main fragment I add this "cards"

What is the difference between Activity and Context?

自闭症网瘾萝莉.ら 提交于 2019-11-27 03:47:51
Are Activity and Context the same, or are there differences? When should I have a method pass an Activity, and when a Context? Rafael T As far as I understand: Context is the Base Object. So every Activity same as Application derives from Context. This means that every Activity and every Application IS a Context ; From developer.android.com Activity java.lang.Object ↳ android.content.Context ↳ android.content.ContextWrapper ↳ android.view.ContextThemeWrapper ↳ android.app.Activity And Application java.lang.Object ↳ android.content.Context ↳ android.content.ContextWrapper ↳ android.app