Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass along context from MenuItem onOptionsItemSelected to ASyncTask?

Edit: The question in one line:

How to pass along context from a MenuItem onOptionsItemSelected?

Edit 2: Here is a global indication of what I'm doing: https://github.com/JakeWharton/ActionBarSherlock/blob/master/samples/fragments/src/com/actionbarsherlock/sample/fragments/FragmentTabsPager.java


I'm building an app based on ActionBarSherlock. It uses tabs, fragments and a viewpager. It's meant to be as versatile as possible. A tab can either contain a webview, or a listview. Here's a screenshot:

http://dl.dropbox.com/u/11218283/Screenshot_2012-05-16-13-33-08.png

When the tab (SherlockListFragment) is first loaded, it reads content from a feed using an ASyncTask. OnPostExecute it updates the list and saves the result to a SharedPreference string (as a way of caching). As you can imagine, the AsyncTask requires context to do this. I'm calling the asynctask from the SherlockListFragment like this:

x.new refreshList(this,getActivity()).execute();

(x being a reference to my current file).

And transfer it to my AsyncTask like this

 public refreshList(TabList a,Context b){
    this.mContext = a;
    this.mCont = b;
}

All that works fine. But as you can see I've added in a menu item for refreshing. The way I've set things up, I'm not capable of re-running the ASyncTask, since I haven't got the appropriate context information. Can anyone tell me how to perform a refresh in this situation?

If anyone willing to help requires to see more code, I'll gladly comply.

Thanks in advance.

like image 817
Vic V Avatar asked May 16 '12 11:05

Vic V


1 Answers

You said you are responding to the button in onOptionsItemSelected, so the context is just this if you are doing it in the Activity. If in a fragment it is getActivity().

like image 72
Jason Hanley Avatar answered Oct 15 '22 05:10

Jason Hanley