0

I have an async task which eventually creates the parameters email,about. The async task is in the onCreate method, and so is this CustomListAdapter. I want to move the CustomListAdapter from onCreate in the asyncTask in onPostExecute.My problem is that this means the context of the application. When I move it in onPostExecute it doesn't get that context anymore. I tried to replace it with getApplicationContext() , activityName.this.getApplicationContext but it doesn't work because it's expecting the context of the application. I found this Static way to get 'Context' on Android? but I can't define a name in the manifest for some reason. What other method of getting the application's context can I use in the onPostExecute method of the asyncTask? Or should I just make an Asynck task with the context as a parameter ?

CustomListAdapter adapter=new CustomListAdapter(this, email,about);
                list=(ListView)findViewById(R.id.list);


 list.setAdapter(adapter);

            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    // TODO Auto-generated method stub
                    String Slecteditem= email.get(position);
                    Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();

                }
            });
Community
  • 1
  • 1
Bogdan Daniel
  • 2,689
  • 11
  • 43
  • 76

2 Answers2

7

If your activities name is MyActivity, you can always just use MyActivity.this to get its context

Tomer Shemesh
  • 10,278
  • 4
  • 21
  • 44
  • That solved it. I tried only `MyActivity.this.getApplicationContext`. It seems that just MyActivity.this gets the context. Thank you. – Bogdan Daniel May 26 '15 at 14:23
1

You can pass context of your Activity component in constructor of your CustomAysncTasklike in below code snippet and use it. But always make proper check for null

MyActivity activity
public CustomAsycTask(MyActivity activity) {
   this.activity=activity;
}

Update: As Tomer Shemesh pointed out

private WeakReference<MyActivity> activity = null;

public CustomAsycTask(MyActivity activity) {
    activity= new WeakReference<MyActivity>(activity);
}

and for accessing it:

final MyActivity iActivity = activity.get();
Mithun
  • 2,075
  • 3
  • 19
  • 26
  • it is very bad practice to pass a reference of your activity to an Async task because if your task runs after your activity has been finished you will either crash or get a memory leak. – Tomer Shemesh May 26 '15 at 14:26
  • And `MainActivity.this` inside `AsyncTask` won't? – Mithun May 26 '15 at 14:29
  • It won't create a memory leak and as long as you do a null check you should be fine – Tomer Shemesh May 26 '15 at 14:53
  • Both are going to create memory issues I guess, and as I said null checks are to be there – Mithun May 26 '15 at 14:55
  • Hmm. i was under the impression that calling Activity.this wasn't going to cause memory issues, because it doesnt stop the activity from being destroyed, but keeping an active reference to the activity will stop garbage collection on the activity. – Tomer Shemesh May 26 '15 at 14:58
  • Okay! But tell me, if your `AsyncTask` is a separate public class and not an inner class, how are you going to access `MainActivity.this` , does it make sense? – Mithun May 26 '15 at 15:13
  • Ahh. i see, yes then you will have to pass in the activity as you said. – Tomer Shemesh May 26 '15 at 15:16