2

I want to go to previous fragment destination when back button is clicke. I have different fragments created for bottom navigation pages with viewpager. here's the code I use currently for the navHost Activity

    @Override
    public void onBackPressed() {


        BaseFragment fragment = fragments.get(main_pager.getCurrentItem());
        boolean hadNestedFragments = fragment.onBackPressed();

        // if no fragments were popped
        if (!hadNestedFragments) {
            if (backStack.size() > 1) {
                // remove current position from stack
                backStack.pop();
                // set the next item in stack as current
                main_pager.setCurrentItem(backStack.peek());

            } else{
//                super.onBackPressed();
            }
        }
    }

Here's the code in BaseFragment

public boolean onBackPressed() {
    return findNavController()
            .navigateUp();
}

When I go back from a fragment to the previous one, I don't want to reload it again I want it to have the state it had just as it was. (Ex: with the same data in recyclerview items and its exact scrolled position ) Any help would be appreciated.

Resad Indipa
  • 161
  • 1
  • 6

0 Answers0