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:
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.
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With