android-context

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

早过忘川 提交于 2019-11-28 17:14:33
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. Somewhere in your AsyncTask you'll want to pass in your activity. Then you'll save that reference in a weak reference. Then you can dereference and use it again in onPostExecute . Class member: WeakReference<Activity> weakActivity;

What is the difference between this, getContext() and getActivity()?

£可爱£侵袭症+ 提交于 2019-11-28 17:14:10
I am very confused with the usage of all these that where should we use them. this - return self reference getContext() - return Context getActivity() - return Activity Context. Quote from original answer : As the name suggests, its 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, package/application) Activity Activity is a Java code that supports a screen or UI. In other words, building block of the user interface is the activity.

Difference in context this and getContext()

假如想象 提交于 2019-11-28 17:13:40
What is difference between this and getContext() , when I say this I mean this within an Activity . 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() function. And you cannot use this as Context in such cases. And these two cases are mutually exclusive. At

Android AsyncTask context behavior

随声附和 提交于 2019-11-28 17:06:48
问题 I've been working with AsyncTasks in Android and I am dealing with an issue. Take a simple example, an Activity with one AsyncTask. The task on the background does not do anything spectacular, it just sleeps for 8 seconds. At the end of the AsyncTask in the onPostExecute() method I am just setting a button visibility status to View.VISIBLE, only to verify my results. Now, this works great until the user decides to change his phones orientation while the AsyncTask is working (within the 8

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

让人想犯罪 __ 提交于 2019-11-28 16:56:49
If I have a reference to Context , is it possible to finish the current activity? I don't have the reference to current activity. yes, with a cast: ((Activity) ctx).finish(); 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 Kartihkraj Duraisamy In my Case following worked, I need to finish my activity in a AsyncTask onPostExcute(). Where my AsyncTask class is separate public class , which has a constructor with param of Context.

How to get a context in a recycler view adapter

房东的猫 提交于 2019-11-28 15:50:47
I'm trying to use picasso library to be able to load url to imageView, but I'm not able to get the context to use the picasso library correctly. public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.ViewHolder> { private List<Post> mDataset; // Provide a reference to the views for each data item // Complex data items may need more than one view per item, and // you provide access to all the views for a data item in a view holder public class ViewHolder extends RecyclerView.ViewHolder { // each data item is just a string in this case public TextView txtHeader; public ImageView pub

How do you obtain a Drawable object from a resource id in android package?

杀马特。学长 韩版系。学妹 提交于 2019-11-28 15:50:14
I need to get a Drawable object to display on an image button. Is there a way to use the code below (or something like it) to get an object from the android.R.drawable.* package? for example if drawableId was android.R.drawable.ic_delete mContext.getResources().getDrawable(drawableId) Pete Houston Drawable d = getResources().getDrawable(android.R.drawable.ic_dialog_email); ImageView image = (ImageView)findViewById(R.id.image); image.setImageDrawable(d); Muhammad Soliman As of API 21 , you should use the getDrawable(int, Theme) method instead of getDrawable(int) , as it allows you to fetch a

Android DataBinding where to get context?

梦想的初衷 提交于 2019-11-28 11:52:23
I have TextView for showing time. I want to use Android's DataBinding plugin. For formatting time I am using DateUtils.formatDateTime(context, int, int) method which takes Context instance. Is it possible to get context include element? Or do I have to use old school way? Thanks Thought I should answer instead of putting in a comment. You'll have more options when rc2 is released. In rc1, you can pass the context in a variable to the Binding, then pass it as a parameter to the method. Alternatively, you can create a custom attribute for data binding: @BindingAdapter({"timeMillis", "dateFlags"}

What is the right way to communicate from a custom View to the Activity in which it resides?

寵の児 提交于 2019-11-28 09:13:13
I have a custom View class that extends Spinner. I'm trying to figure out what the correct way to talk to the Activity that it's embedded in is, when the user makes a selection. I see that the OnItemSelected listener gets a reference to the Adapter, but I'm not clear on whether or not I should be using this adapter and walking up its parent chain somehow, or if I should just talk directly to the context (for some reason that doesn't feel safe, even though I can't think of a way in which it might fail, offhand). Tal Kanel the right way to do that, is to "listen" to your custom view by exposing

Null pointer exception when checking for permission with android.content.Context.checkPermission

半腔热情 提交于 2019-11-28 08:59:26
问题 I need to check for permissions before querying the Android calendar for events. To do it, Android studio is warning that I need to follow a check before querying. The auto generated code is this piece: if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) { System.out.println("NO ACCESS TO CALENDAR!! Abort mission, abort mission!!"); } When trying to run it, I get this error: Attempt to invoke virtual method 'int android.content