保留名单列表片段的方向变化片段、方向、名单、列表

2023-09-04 10:06:36 作者:作业你这个老不死的

我有使用片段的应用程序,所有这些都包含在一个单一的活性。活动开始用含有按钮,所有这些都导致各种listfragments替代原来的按钮/菜单片段的菜单的片段。

我的问题是,在一个方向的变化,如果该活动显示列表视图之一,它消失的按钮菜单回报。我明白为什么会这样......活动被破坏并重新创建,但不知道如何解决它,并保持通过方向更改列表视图/当前片段。

我发现 setRetainInstance 和使用here,但我不知道如何将它应用到我的按钮菜单或可能性的情况,我想保留的部分可在几个不同的人之一。

下面是code简化展现的主要活动和listfragments之一。

任何指针以加什么在哪里让这个列表中的片段将会被保留将大大AP preciated。

活动

 公共类主要扩展FragmentActivity {
    私人MainMenuFragment菜单;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        菜单=新MainMenuFragment();
        。getSupportFragmentManager()的BeginTransaction()代替(R.id.pane,菜单).commit();
    }
}
 

ListFragment

 公共类ItemListFragment扩展ListFragment {

    私人TextView的头;
    私人TextView的空白;
    私人按钮添加;
    公共静态光标itemCursor;
    私人GroceryDB mDbHelper;
    公共静态长mRowId;
    公共静态CheckCursorAdapter名单;

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        视图V = inflater.inflate(R.layout.common_list,集装箱,假);
        标题=(TextView中)v.findViewById(R.id.header);
        空=(TextView中)v.findViewById(android.R.id.empty);
        header.setText(R.string.header_item);
        empty.setText(R.string.empty_items);
        返回伏;
    }

    @覆盖
    公共无效onActivityCreated(包savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        mRowId = 0;
        mDbHelper =新GroceryDB(getActivity());
        mDbHelper.open();

        itemCursor = mDbHelper.fetchAllItems();
        。getActivity()startManagingCursor(itemCursor);

        的String []从=新的String [] {GroceryDB.ITEM_NAME};
        INT []到=新INT [] {R.id.ListItem};
        名单=新CheckCursorAdapter(getActivity()
                R.layout.listlayout_itemlist,itemCursor,从,到);
        setListAdapter(名单);
    }
}
 

解决方案   

如何解决它,并通过方向变化保持列表视图/当前片段

操作系统 第四章 知识点总结

您的每一次的onCreate()被称为盲目更换片段。相反,只添加/替换片段,如果 savedInstanceState()。如果不是,你是来从配置改回来,和您现有的片段将被重新创建(或者,如果他们被保留下来,他们已经在那里)。

setRetainInstance(真)表示,该片段的自身的将整个配置更改保留,而不是被破坏,/重建类的活动。然而,它仍然会调用 onCreateView()。在您的code,这意味着 ItemListFragment 您的数据成员将坚持围绕,但你仍然需要调用 setListAdapter(),即使你不重新查询数据库。

I have an app using fragments, all of which are contained in a single activity. The activity starts with a fragment containing a menu of buttons, all of which cause various listfragments to replace the original button/menu fragment.

My problem is that upon an orientation change, if the activity is displaying one of the listviews, it goes away and the button menu returns. I understand why this is happening... the activity is destroyed and re-created, but not how to work around it and maintain the list view/current fragment through the orientation change.

I've found setRetainInstance and the example of use here, but I can't figure out how to apply it to my situation with the button menu or the possibility that the fragment I want to retain could be one of several different ones.

Below is code simplified to show the main activity and one of the listfragments.

Any pointers in what to add where to make it so that the list fragment will be retained would be greatly appreciated.

Activity

public class Main extends FragmentActivity {
    private MainMenuFragment menu;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        menu = new MainMenuFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.pane, menu).commit();    
    }
}

ListFragment

public class ItemListFragment extends ListFragment {

    private TextView header;
    private TextView empty;
    private Button add;
    public static Cursor itemCursor;
    private GroceryDB mDbHelper;
    public static long mRowId;
    public static CheckCursorAdapter lists;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.common_list, container, false);
        header = (TextView) v.findViewById(R.id.header);
        empty = (TextView) v.findViewById(android.R.id.empty);
        header.setText(R.string.header_item);
        empty.setText(R.string.empty_items);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mRowId=0;
        mDbHelper = new GroceryDB(getActivity());
        mDbHelper.open();

        itemCursor = mDbHelper.fetchAllItems();
        getActivity().startManagingCursor(itemCursor);

        String[] from = new String[] { GroceryDB.ITEM_NAME };
        int[] to = new int[] { R.id.ListItem };
        lists = new CheckCursorAdapter(getActivity(),
                R.layout.listlayout_itemlist, itemCursor, from, to);
        setListAdapter(lists);        
    }
}

解决方案

how to work around it and maintain the list view/current fragment through the orientation change

You are blindly replacing the fragment every time onCreate() is called. Instead, only add/replace the fragment if savedInstanceState() is null. If it is not null, you are coming back from a configuration change, and your existing fragments will be recreated (or, if they were retained, they are already there).

setRetainInstance(true) means that the fragment itself will be retained across configuration changes, instead of being destroyed/recreated like the activity is. However, it will still be called with onCreateView(). In your code, that means that your data members of ItemListFragment would stick around, but you would still need to call setListAdapter() even if you do not requery the database.