我从活动中膨胀了一个片段。如果未启用开发人员设置中的活动选项,则片段不会加载应加载的列表。
片段代码是:
public class WidgetListFragment extends ListFragment {
private DummyAdapter mAdapter;
private LinearLayout mButtonBar;
private DummyCardHost mAppWidgetHost;
private static final List<MyListMenuItem> MY_LIST_MENU_ITEMS = new ArrayList<MyListMenuItem>() {{
add(MyListMenuItem.ADD_WIDGET_STREAM);
}};
/**
* Loader for favorite cards.
*/
private final LoaderManager.LoaderCallbacks<List<Card>> mCardsLoader = new LoaderManager.LoaderCallbacks<List<Card>>() {
@Override
public Loader<List<Card>> onCreateLoader(int id, Bundle args) {
return new MyWidgetsLoader(getActivity());
}
@Override
public void onLoadFinished(Loader<List<Card>> loader, List<Card> cards) {
updateAdapter(cards);
}
@Override
public void onLoaderReset(Loader<List<Card>> loader) {
}
};
public WidgetListFragment() {
super();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAppWidgetHost = new DummyCardHost(getActivity(), WidgetHostIds.MY_WIDGETS);
mAppWidgetHost.startListening();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
mButtonBar = new LinearLayout(getActivity());
mButtonBar.setOrientation(LinearLayout.HORIZONTAL);
mButtonBar.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
AbsListView.LayoutParams.WRAP_CONTENT));
EditableListView listView = (EditableListView) getListView();
listView.addFooterView(mButtonBar);
listView.setOnEditModeListener(this);
listView.setEditableAdapter(null);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getLoaderManager().restartLoader(LOADER_ID, null, mCardsLoader);
}
@Override
public void onDestroy() {
super.onDestroy();
mAppWidgetHost.stopListening();
getLoaderManager().destroyLoader(LOADER_ID);
}
private void updateAdapter(List<Card> cards) {
if (mAdapter != null) mAdapter.clear();
mAdapter = new CardListAdapter(getListView(), this, mAppWidgetHost,
cards, new Space(SpaceConstants.MY_WIDGETS_SPACE_ID));
mAdapter.setMyListMenuItems(mButtonBar, MY_LIST_MENU_ITEMS, true);
((EditableListView) getListView()).setEditableAdapter(mAdapter);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (mAdapter != null) {
mAdapter.onActivityResult(requestCode, resultCode, data);
}
}
活动是:
public class MyWidgetsActivity extends FragmentActivity {
private WidgetListFragment mFragment;
public MyWidgetsActivity() {
super();
DependencyInjectionService.inject(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_widgets);
mFragment = (WidgetListFragment) getSupportFragmentManager().findFragmentById(R.id.widget_fragment);
}
}
为什么杀死之前的活动的任何想法都会使片段过时?