机器人:它是更好的每一个抽屉式导航项被点击时创建一个新片段,或加载了previously创建片段? [英] Android: Is it better to create a new fragment every time a navigation drawer item is clicked, or load up previously created fragments?

查看:132
本文介绍了机器人:它是更好的每一个抽屉式导航项被点击时创建一个新片段,或加载了previously创建片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了Android的标准抽屉式导航模式,其中约10个片段,用户可以从抽屉导航。目前,我的每一个不同的抽屉式导航项被点击像这样的时间创建一个新片段:

I am implementing the standard navigation drawer pattern for android, with about 10 fragments the user can navigate to from the drawer. Currently, I am creating a new Fragment every time a different navigation drawer item is clicked like so:

// When a new navigation item at index is clicked 
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Fragment newFragment = null;
if (index == 0)
    fragment = new Fragment0();
...
ft.replace(R.id.container, newFragment);
ft.commit();

我一直在想,如果这将是更有效地做一些这样的:

I have been wondering if it would be more efficient to do something like the following:

// Somewhere in onCreate
Fragment[] fragments = new Fragment[n];
fragments[0] = new Fragment0();
fragments[1] = new Fragment1();
...

// When a new navigation item (at index) is clicked 
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.replace(R.id.container, fragments[index]);
ft.commit();

我主要担心的是,一些片段持有显著的数据(相当大的列表和大量的意见)的。会不会有任何问题,拿在内存中的所有这些片段,每一次将为其提供任何优势实例化新的片段(除了更快的开关片段之间)?是否有一个普遍接受的更好的解决方案?

My main worry is that some of the fragments hold a significant amount of data (fairly large lists and lots of views). Would there be any issue holding all these fragments in memory and would it provide any advantage over instantiating new fragments every time (apart from faster switches between fragments)? Is there a generally accepted 'better' solution?

推荐答案

我也有类似的问题。我把你类似的方法,并保存负载处理器我做了以下的调整为每次呼吁为一个片段对象创建一个实例: (上下文我在选择信息的方法使用)

I had a similar issue. I took an approach similar to yours, and to save load on processor I made the following tweak to every call to create an instance for a Fragment object: (in context to my use in selectItem method)

switch (position) {
    case 0:
        if (fragmentOne == null)
            fragmentOne = new FragmentOne();
        getSupportFragmentManager().beginTransaction().......
        break;
    case 1:
        if (fragmentTwo == null)
            fragmentTwo = new FragmentTwo();
        getSupportFragmentManager()......

FragmentOne和FragmentTwo是两个不同的片段类和fragmentOne和fragmentTwo被声明为在MainActivity领域的对象

FragmentOne and FragmentTwo are the two different Fragment classes and fragmentOne and fragmentTwo are their objects declared as fields in MainActivity

修改

我想加你怎么能遵循与数组拿着碎片类似的方法。相反,在的onCreate创造新的碎片,做一个项目被点击时。在的onCreate,发送呼叫到抽屉选择要上默认启动选择的片段。

I would like to add how you could follow a similar approach with the array holding the fragments. Instead of creating new fragments in onCreate, do that when an item is clicked. In onCreate, send a call to the drawer to select the fragment which you want selected by default on launch.

//somewhere in onCreate
if (savedInstanceState == null) {
    selectItem(defaultFragment);
}
else
    selectItem(selectedFragment);
//selectItem is method called by DrawerItemClickListener as well


//in selectItem: when navigation item at index is clicked, the listener calls this
switch (index) {
    case 0:
        if (fragments == null) {
            fragments = new Fragment[n];
            fragment[0] = new Fragment0();
        }
        else if (fragments[0] == null) {
            fragments[0] = new Fragment0();
        }
        break;
    ....
}
FragmentTransaction ft = fragmentManager.beginTransaction();
...
ft.replace(R.id.container, fragments[index]);
ft.commit();

没有必要实例上的onCreate的片段,因为这使启动速度慢。如需要时创建它们,之后使用同一个,如果它的存在。在创建新的片段,只有当他们被加载首次。

No need to instantiate the fragments on onCreate since that makes the startup slow. Create them as and when needed and afterwards use the same one if it exists. The new fragments are created only if they are being loaded for the first time.

这篇关于机器人:它是更好的每一个抽屉式导航项被点击时创建一个新片段,或加载了previously创建片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆