android-context

android - changing Activity UI from application class

时间秒杀一切 提交于 2019-11-28 08:55:46
问题 I extended the Application class in order to create singleton-like object in android. in this object I have all the HTTP work with my server, and all the other activities can access it and call methods to GET, POST etc. Code: public class HttpManagerInstance extends Application { private HttpClient httpClient; private HttpGet get; @Override public void onCreate() { httpClient = new DefaultHttpClient(); get = new HttpGet("http://10.100.102.9:8000/users/"); super.onCreate(); } public Void

SharedPreferences application context vs activity context

半城伤御伤魂 提交于 2019-11-28 06:10:21
I am using several SharedPreferences to store data in my app. Some preferences are used in a lot of activites. I know that the SharedPreferences are internally backed by a map for fast read-access and written to sdcard when settings are changed. I wonder which way is better if a sharedpreference is accessed by a lot of activies: Instantiate it in every activity using the activity context. Instantiate it in every activity, but using the application context. Put it in e.g. the Application class and instantiate it only once there, similar to a singleton. If I use 1. solution is there a

How to call method from another class without passing context?

早过忘川 提交于 2019-11-28 06:06:07
问题 I am currently trying to call a method from a utility class that will reference a new cursor created for this utility method. Unfortunately, my new class will not let me create the cursor without context. I have tried numerous ways of passing context from the calling activity, but get null pointer exceptions in most cases. Here is the portion of my code: findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Tools.pickRandomItem(); } }); and in

Can't cast to Application with getApplication method

心已入冬 提交于 2019-11-28 05:08:47
问题 I have class App which contain context of my Application . But when I compiled I got an error in other class in this line: App app = (App) getApplication(); Class App : import android.app.Application; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.media.MediaPlayer; import android.support.v4.app.NotificationCompat; import android.util.Log; import com

Android : References to a Context and memory leaks

拟墨画扇 提交于 2019-11-28 04:09:11
I've read that it is a mistake and a source of memory leaks in Android application to keep a long-lived references to a Context. But I don't understand if it is ok to create a class that looks like this one: public class HelperClass { private Context context; public HelperClass(Context context) { this.context = context; } public void myHelperMethod() { // uses this.context } } And call it from an Activity: public class MyActivity extends Activity { public void onCreate(Bundle savedInstanceState) { HelperClass h = new HelperClass(this); h.myHelperMethod(); } ... } This is fine, and will not

Android Intent Context Confusing

百般思念 提交于 2019-11-28 03:08:20
Can somebody explain this to me please : Intent intent = new Intent(Context, AlarmReceiver.class); I never understood and I seriously think I never will if somebody doesn't try to explain this to me in depth. This whole context thing is so confusing to me. Sometimes it works like this : Intent intent = new Intent(getBaseContext(), AlarmReceiver.class); Sometimes it wont work like that but it accepts only : Intent intent = new Intent(context, AlarmReceiver.class); Sometimes its : Intent intent = new Intent(this, AlarmReceiver.class); etc. etc. and many other. I understand basics of context but

in what case does bindService return false?

对着背影说爱祢 提交于 2019-11-28 03:08:07
问题 I was wondering when does Context.bindService() return false ? I've tried to make the onBind() return null , but it still returns true when bindService is called and the onServiceConnected does not get executed. I also saw this on Google Groups with no response https://groups.google.com/forum/#!topic/android-developers/ZLl56Mz1jYg I also can't find the implementation of bindService, since it is abstract in Context.java and searching for "public boolean bindService" does not yield any useful

Change app language in android 5.0 doesn't work

萝らか妹 提交于 2019-11-28 01:57:44
I'm using this code below to change my app language on button click (changing from french to english for example), it's works fine on android 4.0 + but on 5.0 it doesn't. Locale localeEn = new Locale("en_US"); Locale.setDefault(localeEn); Configuration configEn = new Configuration(); configEn.locale = localeEn; getApplicationContext().getResources().updateConfiguration(configEn, null); this.recreate(); Any clues why please? edit : this is my manifest ( with android:configChanges ) <activity android:name=".activities.LoginActivity" android:configChanges="orientation|locale" android:label="

Why is my context in my Fragment null?

回眸只為那壹抹淺笑 提交于 2019-11-28 01:51:37
I have got a question regarding the usage of context in a fragment. My problem is that I always get a NullpointerException. Here is what i do: Create a class that extends the SherlockFragment. In that class I have an instance of another Helper class: public class Fragment extends SherlockFragment { private Helper helper = new Helper(this.getActivity()); // More code ... } Here is an extract of the other Helper class: public class Helper { public Helper(Context context) { this.context = context; } // More code ... } Everytime I call context.someMethod (e.g. context.getResources() ) I get a

Access to getString() in android.support.v4.app.FragmentPagerAdapter?

蹲街弑〆低调 提交于 2019-11-28 00:24:01
问题 In a class extending android.support.v4.app.FragmentPagerAdapter , is there any way to get access to the Context.getString(..) method without the extending class being an inner class of an activity or passing in some context from the outside? Thanks for any hint! 回答1: From a fragment use : getActivity().getString(...) From an adapter use : getContext().getResources().getString(...) Yes, you need a context to access the resources. 回答2: From an Activity, use: this.getString(R.string.string_name