首页 > 解决方案 > 导航架构组件,包含列表的片段从开始显示,而从另一个片段返回

问题描述

场景:假设底部导航栏包含Home片段,可以打开另一个片段,比如Product Listing。从产品列表片段中,用户可以打开产品详细信息片段以查看任何项目的详细信息。

问题:假设用户滚动了产品列表片段中的列表并到达第 100 个项目并点击一个项目以在产品详细信息片段中查看其详细信息。现在,当用户按下返回按钮以返回产品列表片段时,列表将从头开始显示。

有什么办法或解决办法来克服这个问题。我想在回来时向用户展示第 100 个项目。

产品列表片段的重写方法 Onstart()、OnCreateView()、OnViewCreated()、OnResume() 在从产品详细信息片段返回时执行。

View view;
Unbinder unbinder;
@Override
    public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.product_listing_screen, container, false);
            return view;
        } else {
            return view;
        }
    }

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Timber.e("%s onViewCreated Called ", TAG);
       unbinder = ButterKnife.bind(this, view);
    }

    @Override
        public void onResume() {
            super.onResume();
            Timber.e("%s onResume Called ", TAG);
     if (adapter == null || adapterList.size() == 0) {
                updateProductListingCategory(itemId); // This is the method making a web request. And while coming back from detail fragment, it isn't called again.
                }
}

标签: javaandroid-studioandroid-fragmentsandroid-architecture-componentsandroid-architecture-navigation

解决方案


推荐阅读