views:

18

answers:

1

This function runs on the UiThread, but it seems that doesnt mean it also has access to the activity context. Shall I implement to my AsyncTask a public Setter to pass the activity (as reference)?

protected void onPostExecute( Bitmap bitmap ) {
         //following is underlined red due to missing context
        (ImageView)findViewById(ResId)).setImageBitmap(bitmap); 
      }
A: 

I can think about two options.

  1. If the AsyncTask is an inner class of you activity you can use getApplicationContext() to get the Context object, without passing the activity object.
  2. If you have to access the ImageView object from different methods or if the task handled by the AsyncTask class can run multiple times you might think about storing the ImageView object as a member of your activity class, so you can don't need a context to access it.
Flo
@1: I created the class in a new file, because I want to use this generic class in all of my Activities. So, I guess thats not going to work. However, I am already storing a reference of the ApplicationContext in my "AppController", which I can access from any point of my Code. But there is not much difference by passing the ActivityContext to the ApplicationContext. Or, is there? I sometimes felt, ApplicationContext is kinda slower. On top of that, I dont actually think u can access the findViewById-Method thorugh the applicationContext which is not the Context of the Activity.
OneWorld
@2: So here you suggest me not to pass the context, but pass just the reference of the desired ImageView object. I think thats a good way, because I need the context just for "findViewById". Is it more expensive in terms of Performance to pass the Context rather than passing just the ImageView object?
OneWorld
Ok, then option 2 would be the better solution I guess. I don't know exactly which object type needs more resources but for me a Context object seems to be much "heavier" than one single ImageView.
Flo