使用FragmentTransaction.attach()和detach()时不保留片段ui

时间:2013-03-03 07:56:23

标签: android android-fragments detach

根据我对文档的理解,您可以分离片段并稍后重新附加它,它将自动看起来与分离时相同。还有更多吗?我有一个简单的操作栏,带有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;
}

}

3 个答案:

答案 0 :(得分:1)

使用show and hide而不是attach和detach。这是一个示例代码,

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

答案 1 :(得分: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事件时调用commit。

答案 2 :(得分:-1)

默认情况下,分离然后附加Fragment时会重新创建setRetainInstance(true)。您可以通过调用Fragment上的Fragment来更改此行为:实际实例将保留在内存中,并且在分离附加周期后不会重新创建。但是Fragment的用户界面会以两种方式被破坏并在此之后重新创建,因此您需要确保在将Activity重新连接到{{1}}后使用适当的数据填充视图。希望这会有所帮助。