android-context

Does the Android Application Context ever get destroyed?

限于喜欢 提交于 2019-12-22 03:52:24
问题 I use my Android app's application Context as a storage area for "current state" information for my app. I'm finding that in the field, there are cases when this information goes away on some people's devices causing various NullPointerExceptions since I expect the data to be there when the app resumes and starts rebuilding the necessary activities. This usually happens when the user hits "Home", does something else, then eventually wanders back into the app - it attempts to go back to where

What to do when context is null in fragment

半世苍凉 提交于 2019-12-22 01:18:23
问题 Is it safe (correct) to simply return from method's and do nothing if context is null? currently I'm doing this when ever I need context from fragment. Context context = getContext(); if(context == null) return; My question is too stupid and obvious but I'm still not sure if this is correct. if its necessary or not. (maybe I should log warnings?) I do this check in onCreateView , onViewCreated , when getting context from itemView in view holders and inside view click listeners when ever

How to provide Context to call necessary functions in my own utility class

谁说我不能喝 提交于 2019-12-22 00:09:24
问题 Sometimes I need to provide a Context object to call specific functions, such as Intent intent = new Intent(context, MyClass.class); in order to start a service context.startService(intent); Or, provide a Context object to do a query Cursor cursor = context.managedQuery(uri, projection, null, null, null); If this is done in a UI class which extends Activity, that's fine. However, If I want to create my own utility class (a singleton) that doesn't extend anything and call these functions, I

Passing context to Handler

时光总嘲笑我的痴心妄想 提交于 2019-12-21 17:39:09
问题 Is it possible to pass arguments to an Android Handler?? I have two pieces of code. new Thread(){ public void run(){ for(;;){ uiCallback.sendEmptyMessage(0); Thread.sleep(2000); //sleep for 2 seconds } } }.start(); private Handler uiCallback = new Handler(){ public void handleMessage(Message msg){ //add a new blossom to the blossom ArrayList!! blossomArrayList.add(new Blossom(context, R.drawable.blossom)); } }; I of course get an error because the Handler method cannot see my context. This is

How can I call getContentResolver in android?

佐手、 提交于 2019-12-21 07:34:06
问题 I'm writing a library class to encapsulate some of my logic in my first Android app. One of the functions which I want to encapsulate is a function which queries the address book. As such, it needs a ContentResolver . I'm trying to figure out how to keep the library functions black-boxed... that is, to avoid having each Activity pass in its own context to get a ContentResolver . Problem is I cannot for the life of me figure out how to get a ContentResolver from within my library function. I

How to pass an Activity instance to another activity

扶醉桌前 提交于 2019-12-21 06:29:22
问题 Hi I have an activity named BaseActivity , which extends Activity. from this i have to go to SettingsActivity which extends PreferenceActivity , on menu button press. To start a AsyncTask , which is in an independent class, i need an instance of BaseActivity . How can i get a BaseActivity instance in the SettingsActivity ? is there any way like, eg: intent.putExtra("activity_instance",BaseActivity.this); 回答1: Use getters and setters and make the class they reside as singleton class. This is a

How to pass an Activity instance to another activity

ⅰ亾dé卋堺 提交于 2019-12-21 06:29:09
问题 Hi I have an activity named BaseActivity , which extends Activity. from this i have to go to SettingsActivity which extends PreferenceActivity , on menu button press. To start a AsyncTask , which is in an independent class, i need an instance of BaseActivity . How can i get a BaseActivity instance in the SettingsActivity ? is there any way like, eg: intent.putExtra("activity_instance",BaseActivity.this); 回答1: Use getters and setters and make the class they reside as singleton class. This is a

passing context as argument of DialogFragment

主宰稳场 提交于 2019-12-20 09:11:29
问题 it's possible to pass a context variable to a DialogFragment? i've use this code inside dialog for passing a string: public static ConfirmDialog newInstance( String f) { ConfirmDialog d = new ConfirmDialog(); Bundle args = new Bundle(); args.putString("FILE_NAME", f); d.setArguments(args); return d; } but i don't find any function like putString for passing context. It's possible to do that? 回答1: Your DialogFragment has a very handy method for getting a Context instance: getActivity()

get Activity object while in View context

二次信任 提交于 2019-12-19 09:58:03
问题 This is a followup to this post: findViewById in a subclassed SurfaceView throwing RuntimeException Based on Romain Guy's feedback (which I'll accept shortly as it is a solution), I'd like to obtain the calling Activity from within the View, so that I can use it to obtain the desired TextView resource. I don't see any methods in View that return Activity. What is the proper way to do this? Or is there a better alternative for working with TextViews from within another View context. Basically,

Do not place Android context classes in static fields; this is a memory leak

时光怂恿深爱的人放手 提交于 2019-12-19 09:28:43
问题 I have a service which has a BeaconNotificationsManager , I want to access this BeaconNotificationsManager in my Activity . Currently my BeaconNotificationsManager is static : public class MyService extends Service { public static BeaconNotificationsManager bnm; } And I am accessing this in my Activity like this: if(MyService.bnm != null){ // do stuff } Although Android is telling me this is bad. What is the correct way to do this? 回答1: About Static issue: let just say you are referencing