0

I use a master/detail flow and in every fragment I have a viewpager containing some forms but the thing is when I click on some other links from the master list, the content of the viewpager is lost. How to save this content so everytime I click on the same choice I find my data.
My code :

@Override
    public void onItemSelected(String id) {
        if (mTwoPane) {
            // In two-pane mode, show the detail view in this activity by
            // adding or replacing the detail fragment using a
            // fragment transaction.

            if(id.contains("1"))
            {
                Bundle arguments = new Bundle();

                ItemDetailFragment fragment = new ItemDetailFragment();
                fragment.setRetainInstance(true);
                fragment.setArguments(arguments);
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.item_detail_container, fragment).commit();
            }
            else if(id.contains("2"))
            {
                Bundle arguments = new Bundle();

                ItemDetailFragment2 fragment = new ItemDetailFragment2();
                fragment.setRetainInstance(true);
                fragment.setArguments(arguments);
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.item_detail_container, fragment).commit();
            }
        } else {
            // In single-pane mode, simply start the detail activity
            // for the selected item ID.
            Intent detailIntent = new Intent(this, ItemDetailActivity.class);
            detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
            startActivity(detailIntent);
        }
    }

EDIT: I tried to modify my code this way :

if (id.contains("1")) {
Fragment currFragment = fm.findFragmentByTag(lastTag);

                ItemDetailFragment newFragment = (ItemDetailFragment) fm.findFragmentByTag("f"+1);
                FragmentTransaction ft = fm.beginTransaction();

                ft.hide(currFragment);

                    if(newFragment==null)
                    {lastTag="f"+1;

                    newFragment=new ItemDetailFragment();
                    ft
                    .replace(R.id.item_detail_container, newFragment);}
                    else {
                        ft.show(newFragment);
                    }


                ft.commit();
}

But i got a NullPointerException at android.support.v4.app.BackStackRecord.run(BackStackrecord.java:656)

4

3 回答 3

0

最好保存在arrayList中。Onclick 将图像 id 存储在 arrayList 中。

于 2013-09-20T10:38:50.923 回答
0

尝试在您的片段 onCreate() 中调用 setRetainInstance(true)

这里所写,如果您不将事务添加到后台堆栈,则该片段在删除或替换时将被销毁。您可以隐藏当前片段,而不是替换,然后添加新片段。像那样:

int currPage = 0;

public void goToPage(int num) {
    FragmentManager fm = getSupportFragmentManager();
    Fragment currFragment = fm.findFragmentByTag("f" + currPage);
    Fragment newFragment = fm.findFragmentByTag("f" + num);
    FragmentTransition ft = fm.beginTransaction();

    ft.hide(currFragment);

    if(newFragment == null) {
        // First use. Create new instance for page.
        newFragment = new MyFragment();
        ft.add(R.id.item_detail_container, newFragment, "f" + num);
    } else {
        // Fragment for page already added. Just show it.
        ft.show(newFragment);
    }

    ft.commit();
    this.currPage = num;
}

并且不要忘记为初始页面片段指定标签。

于 2013-09-20T12:02:38.653 回答
0

您可以实现片段的onPause()oronStop()方法,并在调用该方法时保存数据,这会在它们被删除时自动发生。

或者您可以使用已保存的InstanceState。

于 2013-09-20T10:51:05.263 回答