【问题标题】:Android Fragments should I reuse 1 fragment or create new instances?Android Fragments 我应该重复使用 1 个片段还是创建新实例?
【发布时间】:2012-03-24 22:41:39
【问题描述】:

我正在尝试在 Android 中学习 Fragments,从各种示例中我发现似乎有不同的方法,我只是想就哪种方法正确,或者至少在哪种情况下获得一些建议应该使用另一种方式。

一个示例创建了一个包含片段和 FrameLayout 的布局。在代码中,当从 ListFragment 中选择一个项目时,会创建一个新的 Fragment(在构造函数中需要一些数据)并将 FrameLayout 替换为这个新的 Fragment(使用 FragmentTransaction.replace())。

另一个示例有一个布局文件,它并排声明了 2 个片段。现在在代码中,当用户从一个片段的列表中选择一个项目时,会调用另一个片段来更新数据(基于所选项目)。

所以我只是想知道这两种方法中的任何一种是否比另一种更受欢迎,或者是否在某些情况下应该使用一种方法?

编辑:这是我所指的两种方法的代码:

1:

        mCurCheckPosition = index;

        if (mDualPane) {
            // We can display everything in-place with fragments, so update
            // the list to highlight the selected item and show the data.
            getListView().setItemChecked(index, true);

            // Check what fragment is currently shown, replace if needed.
            DetailsFragment details = (DetailsFragment)
                    getFragmentManager().findFragmentById(R.id.details);
            if (details == null || details.getShownIndex() != index) {
                // Make new fragment to show this selection.
                details = DetailsFragment.newInstance(index);

                // Execute a transaction, replacing any existing fragment
                // with this one inside the frame.
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.details, details);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
            }

        } else {
            // Otherwise we need to launch a new activity to display
            // the dialog fragment with selected text.
            Intent intent = new Intent();
            intent.setClass(getActivity(), DetailsActivity.class);
            intent.putExtra("index", index);
            startActivity(intent);
        }

2:

public void onListItemClick(ListView l, View v, int position, long id) {
    String item = (String) getListAdapter().getItem(position);
    DetailFragment fragment = (DetailFragment) getFragmentManager()
            .findFragmentById(R.id.detailFragment);
    if (fragment != null && fragment.isInLayout()) {
        fragment.setText(item);
    } else {
        Intent intent = new Intent(getActivity().getApplicationContext(),
                DetailActivity.class);
        intent.putExtra("value", item);
        startActivity(intent);

    }

}

【问题讨论】:

    标签: android android-layout android-fragments


    【解决方案1】:

    所以我只是想知道这些方法中的任何一种是否比另一种更受欢迎,或者是否在某些情况下应该使用一种方法?

    如果实际片段不需要更改(即,它是相同的片段类),我会让活动调用该片段上的方法而不是替换它(您的场景 #2),假设它存在。这在运行时成本要低得多,而且编码也可能更简单。

    但是,如果片段可能需要是不同的片段(例如,根据您单击的内容,列表中表示的不同类型的模型对象可能有不同的片段),则需要替换片段(你的场景#1)。您可以优化该事件的片段 发生 属于同一类的情况,尽管我首先会专注于仅通过替换片段来使其正常工作,并担心如果/何时进行优化有时间和兴趣。

    不过,我在结构上不喜欢您的#2 代码。恕我直言,片段不应该直接与其他片段交谈。我更喜欢的模式是片段“坚持他们的编织”,只关注他们自己的小部件和模型中的东西。对于影响 UI 其他部分的事件(例如,列表单击),让片段通知活动(例如,通过侦听器接口)。活动是知道哪些片段应该在周围的活动,因为它是首先创建它们的活动。然后该活动可以与另一个片段(如果存在)对话,创建另一个片段(如果有空间),或者启动另一个活动。如果您更喜欢 #2 方法,欢迎您使用它——在您的情况下,我不会这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 2019-09-23
      • 2018-04-10
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多