Android開(kāi)發(fā)之Fragment動(dòng)態(tài)使用

Android開(kāi)發(fā)之奇怪的Fragment 一文中,講到了Fragment的基礎(chǔ)知識(shí),這次來(lái)重點(diǎn)討論一下Fragment的動(dòng)態(tài)使用。這種情況主要針對(duì)布局文件中用占位符,在Activity中進(jìn)行切換Fragment的情況,分為兩種:

  • 1、使用replace方法把原有的Fragment替換掉;
  • 2、使用hideshow方法,把已經(jīng)添加過(guò)的Fragment再次隱藏或顯示出來(lái)。

下面主要探討一下這兩種情況之間的區(qū)別

1、使用replace方法把原有的Fragment替換掉

public class MainActivity extends ActionBarActivity implements OnClickListener
{

    private Button ott, tto;
    Fragment1 f1;
    Fragment2 f2;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ott = (Button) findViewById(R.id.ott);
        tto = (Button) findViewById(R.id.tto);

        ott.setOnClickListener(this);
        tto.setOnClickListener(this);

        f1 = new Fragment1();
        f2 = new Fragment2();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(R.id.fl, f1);
        ft.commit();
    }

    @Override
    public void onClick(View arg0)
    {
        if (arg0.getId() == R.id.ott)
        {

            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.fl, f2);
            ft.commit();

        }
        else if (arg0.getId() == R.id.tto)
        {

            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.fl, f1);
            ft.commit();

        }
    }
}

這種情況

(1)程序剛剛啟動(dòng)


replace init.PNG

(2)Fragment1 ——>Fragment2,生命周期為:

replace oneTtwo.PNG

(3)Fragment2 ——>Fragment1,生命周期為:

replace twoTone.PNG

(4)再次Fragment1 ——>Fragment2,生命周期為:


replace oneTtwo.PNG

如上,若頻繁地replace Fragment來(lái)切換,會(huì)不斷創(chuàng)建新實(shí)例,銷毀舊的,浪費(fèi)資源,無(wú)法重用。

使用場(chǎng)景:如果被替換的Fragment無(wú)須再次使用,可以使用replace方法。

</br>


2、使用hideshow方法,把已經(jīng)添加過(guò)的Fragment隱藏或顯示出來(lái)。

public class MainActivity extends ActionBarActivity implements OnClickListener
{

    private Button ott, tto;
    Fragment temp;
    Fragment1 f1;
    Fragment2 f2;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ott = (Button) findViewById(R.id.ott);
        tto = (Button) findViewById(R.id.tto);

        ott.setOnClickListener(this);
        tto.setOnClickListener(this);

        f1 = new Fragment1();
        f2 = new Fragment2();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(R.id.fl, f1);
        ft.commit();

        temp = f1;
    }

    @Override
    public void onClick(View arg0)
    {
        if (arg0.getId() == R.id.ott)
        {
            switchFragment(f2);

        }
        else if (arg0.getId() == R.id.tto)
        {
            switchFragment(f1);

        }
    }

    /**
     * hide和show切換Fragment
     * 
     * @param fragment
     */
    private void switchFragment(Fragment fragment)
    {
        // temp默認(rèn)為Fragment1
        if (fragment != temp)
        {
            if (!fragment.isAdded())
            {
                getFragmentManager().beginTransaction().hide(temp)
                        .add(R.id.fl, fragment).commit();
            }
            else
            {
                getFragmentManager().beginTransaction().hide(temp)
                        .show(fragment).commit();
            }
            temp = fragment;
        }
    }
}

此時(shí)情況會(huì)發(fā)生什么變化呢?
(1)程序剛剛啟動(dòng)

hideAndShow init.PNG

(2)Fragment1 ——>Fragment2,生命周期為:

hideAndShow oneTtwo.PNG

(3)Fragment2 ——>Fragment1,不打印任何生命周期
(4)再次Fragment1 ——>Fragment2,不打印任何生命周期

使用場(chǎng)景:如果Fragment需要重用或者不斷切換,可以使用該方法提高性能。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容