android-context

BroadcastReceiver in Singleton class not receiving intents

空扰寡人 提交于 2019-12-12 04:06:48
问题 I have a Broadcast receiver in a singleton class that's not receiving Broadcast intents. The singleton is initialized with a context (i.e. getInstance(context)). I call getContext().sendBroadcast(intent); but the BroadcastReceiver doesn't get anything. I've confirmed the intent filters are matching. The way I register the receiver is in my singleton constructor like so private class Singleton(Context context) { context.registerReceiver(mReceiver, INTENT_FILTER); .... private onDestroy(Context

Glide with Fragment Context

我的未来我决定 提交于 2019-12-12 03:48:19
问题 This is related to Glide image loading with application context I have several Fragments hosted in an Activity, with a Fragment being replaced by another as the user navigates through the app. I'm passing a RequestManager into my MyFragment's RecyclerView adapter like so: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ... MyAdapter adapter = new MyAdapter(Glide.with(this), listOfPhotos); recyclerView.setAdapter(adapter); ... } My

Correctly handling fragment interactions from an Async-Task

…衆ロ難τιáo~ 提交于 2019-12-12 02:54:59
问题 I am trying to refactor my fragment-heavy code with safer async tasks as I had noticed some occasional crashes. Now I am slightly better educated after some searches I've noticed my main problem is accessing activity or fragment variables from within the async task itself. I am separating each async task into single classes, which implement interface callbacks to the fragment or activity which called them. For example: public class LoginFragment extends Fragment implements

Android AsyncTask Context Terminated

橙三吉。 提交于 2019-12-11 22:26:29
问题 When an Activity terminates, e.g. after screen orientation changing, is that possible to change an AsyncTask activity context? Else it will create an error because when the activity terminates AsyncTask 's activity context is gone too. My homework done is the following: public void onSaveInstanceState(Bundle savedInstanceState) <- doesn't solve public Object onRetainNonConfigurationInstance() <- doesn't solve android:configChanges="keyboardHidden|orientation" <- solved but doesn't handle well

getResources() from static method, without context

為{幸葍}努か 提交于 2019-12-11 13:14:40
问题 I'm working on a SQLiteOpenHelper from which I'll read databases via static methods (since the databases are shared anyway). Is it possible to get the application context to something like: public static final Context context = XXX; It should be possible right? Since I'm obviously only calling from the current app and both resources and databases are shared inside the app. To be clear: I want to access Resources and the SQLiteDatabases (if I happen to be wrong about the context approach). Is

Android: getApplicationContext() won't work in View subclass

时光怂恿深爱的人放手 提交于 2019-12-11 12:33:02
问题 So i have made a class DrawView which extends View and i want to draw some graph in that class with the points i stored as int array i made this array like a sort of public variable with the help of How to declare global variables So when i want to get connected with MyApp in Activity and change my array, i simply use MyApp appState = ((MyApp)getApplicationContext()); but the problem is that this won't work when i call it in my DrawView.java class. Any ides how to solve this? 回答1: I really

StartActivity from different context

江枫思渺然 提交于 2019-12-11 12:19:20
问题 I've got a main class and a separete class (OnClickBtn.java) in which I want to keep just the button methos. I would like to start new activities not from the main class but from the OnClickBtn.java. I've run the program but it crashes at launch. I guess that my main issue is related to the code for the Intent object "Intent int_btnOpenA =new Intent(objContex, ActivityA.class)" This is my code, MainActivity.java: package com.example.and2dtest; import android.app.Activity; import android.os

Null in while using context menu for recyclerview

ぐ巨炮叔叔 提交于 2019-12-11 11:31:40
问题 I have read a lot of STACK questions and tried implementing everything possible. but i keep getting null for AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; Nothing seems to work for me. Trying to get the position of the the item in recyclerview when long clicked. public class CheckOutstandingPayment extends AppCompatActivity { ClsWebConnection oClsWeb = new ClsWebConnection(); DbConnection oDbCon; private String strLeadID; private ArrayList<String>

Getting Main Activity from Main Application

耗尽温柔 提交于 2019-12-11 11:07:47
问题 I have the following manifest. <application android:name=".MyMainApplication" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MyMainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> I also have the following global static function. MyMainApplication application =

Can storing a system service statically lead to memory leaks?

青春壹個敷衍的年華 提交于 2019-12-11 07:57:59
问题 We all know that storing a context object (other than the application context) statically is bad practice because it can lead to memory leaks. But can you store a system service derived from the context object? Such as ConnectivityManager ? // Is this okay? static ConnectivityMananger connectivityManager; ... connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 回答1: I recommend using the application Context for getting such a system service: