如何保存片段中包含的viewpager内容?

时间:2013-09-20 10:31:58

标签: android android-viewpager

我使用主/详细信息流,在每个片段中我都有一个包含某些表单的viewpager,但事实是当我点击主列表中的其他链接时,viewpager的内容迷路了。如何保存这些内容所以每次点击相同的选项我都会找到我的数据 我的代码:

@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);
        }
    }

修改 我试着用这种方式修改我的代码:

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();
}

但我在NullPointerException

获得android.support.v4.app.BackStackRecord.run(BackStackrecord.java:656)

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

或者你也许可以使用savedInstanceState。

答案 2 :(得分:0)

尝试在片段onCreate()

中调用setRetainInstance(true)

作为writed here,如果您不将事务添加到后台堆栈,则在删除或替换时会破坏该片段。 您可以隐藏当前片段,而不是替换,然后添加新片段。像那样:

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;
}

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