【问题标题】:How to avoid the layout inflation each time a fragment transaction is committed?每次提交片段事务时如何避免布局膨胀?
【发布时间】:2017-08-03 14:59:03
【问题描述】:

我正在使用片段来设计我的屏幕。
当我导航回另一个片段(从后台堆栈)时,每次都会调用 onCreateView(...) 方法,即使片段已创建。

如何避免方法 onCreateView(...) 每次都被调用并确保它只被调用一次(当它第一次创建时)?

【问题讨论】:

  • 您能否在帖子中包含您的片段代码。第一个和第二个片段。

标签: android android-fragments layout-inflater fragment-oncreateview


【解决方案1】:

如果需要,您可以将膨胀视图缓存到本地字段。例如:

public class ExampleFragment extends Fragment {

    private View fragmentView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        if (fragmentView == null) {
            fragmentView = inflater.inflate(R.layout.you_super_view_id, container);
        }
        return fragmentView;
    }
}

但实际上,寻呼机重新膨胀视图是可以的,因为它当时只将所有片段的一部分保留在内存中。所以,我认为最好的办法是让它发挥应有的作用

【讨论】:

    猜你喜欢
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    相关资源
    最近更新 更多