4

Scenario: Assume that the bottom navigation bar contains Home fragment which can open another fragment say Product Listing. From Product Listing fragment, user can open Product Detail fragment in order to see the details of any item.

Problem: Suppose user has scrolled the list in Product Listing fragment and reached at 100th item & tapped an item to see its detail in Product Detail fragment. Now when user presses back button in order to go back in Product Listing fragment, list is being shown from start.

Is there any way or work around to overcome this issue. I want to show the 100th item to user while coming back.

Overrided method Onstart(), OnCreateView(), OnViewCreated(),OnResume() of Product Listing fragment are executed while coming back from Product Detail fragment.

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.
                }
}
Umair
  • 438
  • 1
  • 8
  • 19
  • Hi, I have answered this earlier. Hope this helps. https://stackoverflow.com/a/56615160/2393309 – Badhrinath Canessane Jul 03 '19 at 12:05
  • @BadhrinathCanessane Tried this as well. But this is not my issue. Data refetching is already solved. The problem is that recyclerview which is containing list of items loses its position & coming back to the fragment shows the list from start. That is unacceptable behaviour. This above link doesn't solve this problem. Its maintains the view but not the list position. – Umair Jul 03 '19 at 13:26
  • 1
    Okay, then I suspect the issue is where you set the ListView data in onCreateView(). Unacceptable behaviour is because of unacceptable/missing code :). Are you using ViewModel ? Please share the code of your fragment and ViewModel and/or Repo code. LiveData/Flowable could also be a possible suspect. – Badhrinath Canessane Jul 03 '19 at 13:49
  • @BadhrinathCanessane just added some methods to show the flow ... And by unacceptable, i meant that in any case, i dont want this thing :). Data fetched from the web request isn't a problem in my case, put a check about adapterList size & only make request if it is 0 or adapter is null. Recyclerview is losing its position when coming back which i guess doesn't involve ViewModel as a mandatory thing here. Correct me if i am wrong! – Umair Jul 04 '19 at 05:11
  • 1
    Theoretically, scroll position is persisted as part of the LayoutManager's savedInstanceState. So if the adapter is only set when data is actually available, then it should not forget scroll position. – EpicPandaForce Jul 04 '19 at 09:51

0 Answers0