我在root活动中有多个片段,我将它们设置为true以保留配置更改时的实例。 然而,这对我来说是一个严重的问题;
我的Fragment.onResume(...)中有一个AsyncTask,它执行后台作业并调用getSherlockActivity()。invalidateOptionsMenu(...)方法来更改操作栏项。当应用程序首次打开片段按预期工作时,无效选项菜单会调用Fragment.oncreateOptionMenu(...)方法。但是当我更改方向时,AsyncTask再次启动并执行此后台作业但无效则不会调用Fragment.oncreateOptionMenu(...),因此我的操作栏仍处于不需要的状态。
这是我的任务;
private class ContactListLoader extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.d("Contact List Fragment", "Task started...");
ContactListFragment.mIsRunning = true;
if (emptyView != null) {
if (emptyView instanceof TextView) {
((TextView) emptyView).setText(getResources().getString(
R.string.contact_list_loading_list_title));
}
}
getSherlockActivity().invalidateOptionsMenu();
getSherlockActivity().setSupportProgressBarIndeterminateVisibility(
true);
}
@Override
protected Void doInBackground(Void... params) {
createDataSet();
ContactListFragment.mIsRunning = false;
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
mContactsListView.setAdapter(mAdapter);
mIsContactsLoadedFirstTime = true;
if (emptyView != null) {
if (emptyView instanceof TextView) {
((TextView) emptyView).setText(getResources().getString(
R.string.contact_list_empty_list_title));
}
}
getSherlockActivity().invalidateOptionsMenu();
getSherlockActivity().setSupportProgressBarIndeterminateVisibility(
false);
if (mContactsLocal != null & !mContactsLocal.isEmpty()) {
listener.onContactListLoaded(mContactsLocal.get(0),
mContactsLocal);
}
try {
if (mCapabilityApi != null
&& mCapabilityApi.isImsConnected(getSherlockActivity()
.getApplicationContext())) {
mCapabilityApi.refreshAllCapabilities();
}
} catch (ClientApiException e) {
e.printStackTrace();
}
Toast.makeText(getSherlockActivity().getApplicationContext(),
getResources().getString(R.string.contact_list_updated),
Toast.LENGTH_SHORT).show();
}
@Override
protected void onCancelled() {
Log.d("Contact List Fragment", "task cancelled 2");
ContactListFragment.mIsRunning = false;
super.onCancelled();
}
}
有什么想法吗?