1

根据我对文档的了解,您可以分离一个片段并稍后重新附加它,它会自动看起来与分离时相同。还有比这更多的吗?我有一个简单的操作栏,带有 2 个按钮,用于 2 个不同的片段。当我从第一个切换到第二个回到第一个时,第一个片段中加载的图像不再可见。我是否错误地使用了附加/分离,或者我是否需要自己管理 instanceState 并重绘 ui。如果是这样,附加和分离方法的意义何在?

   @Override
public void onStart(){
    super.onStart();    
    ndi=new NasaDailyFragment(this);
    bnf=new BreakingNewsFragment(this);
   ft=getFragmentManager().beginTransaction();
   ft.add(R.id.focused_view_container,ndi).commit();

}

public void onTabSelected(Tab tab, FragmentTransaction f) {

switch(tab.getPosition()){
case 0:
    ft=getFragmentManager().beginTransaction();
    if(!ndi.isAdded()){ 
        ft.replace(R.id.focused_view_container, ndi).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
        ft.attach(ndi);
        getFragmentManager().executePendingTransactions();
    }

    break;
case 1:
        ft=getFragmentManager().beginTransaction();
        ft.detach(ndi);
        ft.replace(R.id.focused_view_container, bnf).commit();
        getFragmentManager().executePendingTransactions();
     break;
}

}

4

3 回答 3

1

使用显示和隐藏而不是附加和分离。这是一个示例代码,

 private class MyTabListener implements ActionBar.TabListener {
    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        switch (tab.getPosition()) {
        case 0:

            if (frag1 == null) {
                // If not, instantiate and add it to the activity
                frag1 = Fragment.instantiate(getApplicationContext(),
                        FeedsActivity.class.getName());
                ft.add(android.R.id.content, frag1, "Feeds");
            } else {
                // If it exists, simply attach it in order to show it
                ft.show(frag1);
            }
            return;

        case 1:
            if (frag2 == null) {
                // If not, instantiate and add it to the activity
                frag2 = Fragment.instantiate(getApplicationContext(),
                        ProfileActivity.class.getName());
                ft.add(android.R.id.content, frag2, "Profile");
            } else {
                // If it exists, simply attach it in order to show it
                ft.show(frag2);
            }
            return;
        case 2:

            if (frag3 == null) {
                // If not, instantiate and add it to the activity
                frag3 = Fragment.instantiate(getApplicationContext(),
                        History.class.getName());
                ft.add(android.R.id.content, frag3, "History");
            } else {
                // If it exists, simply attach it in order to show it
                ft.show(frag3);
            }

            return;

        }

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        if (frag1 != null) {
            // Detach the fragment, because another one is being attached
            switch (tab.getPosition()) {
            case 0:
                ft.hide(frag1);
                return;
            case 1:
                ft.hide(frag2);
                return;
            case 2:
                ft.hide(frag3);
                return;

            }

        }
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
    }
}
于 2013-04-18T07:27:18.807 回答
0

你的逻辑有问题 - 你替换片段然后再次附加它,或者如果 1 分离然后替换,那是多余的。除了替换你每次切换标签时都会破坏旧片段。我宁愿这样:

public void onTabSelected(Tab tab, FragmentTransaction f) {
    switch(tab.getPosition()){
    case 0:
        if(ndi.isAdded()){ 
            f.detach(bnf);
            f.attach(ndi);
        }else{
            f.add(R.id.focused_view_container, ndi).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        }
    break;
    case 1:
        if(bnf.isAdded()){ 
            f.detach(ndi);
            f.attach(bnf);
        }else{
            f.add(R.id.focused_view_container, bnf).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        }
     break;
}

而且您不必在传递的 FragmentTransaction OnTabSelected 事件上调用提交。

于 2013-08-02T14:04:22.660 回答
-1

默认情况下,Fragment当您分离然后附加它时会重新创建。setRetainInstance(true)您可以通过调用来更改此行为Fragment:实际实例将保存在内存中,并且在分离-附加循环后不会重新创建。但是. Fragment_ Fragment_ Activity希望这可以帮助。

于 2013-03-03T09:19:29.983 回答