0

I have three tabs, in one tab I request data form backend. When re click this tabs, it reload again. I want when I reload fragment not recreate, all things in listView in memory. My code like this:

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment fragment = null;
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    fragment = FirstFragment.newInstance();
                    break;
                case R.id.navigation_dashboard:
                    fragment = SecondFragment.newInstance();
                    break;
                case R.id.navigation_notifications:
                    fragment = ThreeFragment.newInstance();
                    break;
            }
            if (fragment != null) {
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.content, fragment);
                fragmentTransaction.commit();
            }

            return true;
        }
    });

But when I Symtems.print.out I see result: - Tab1 clicked

  I/System.out: onCreate main
  I/System.out: onAttach 1
  I/System.out: onCreate 1
  I/System.out: onCreateView 1
  I/System.out: onResume main
  I/System.out: onResume 1

-Tab2 clicked

 I/System.out: onAttach 2
 I/System.out: onCreate 2
 I/System.out: onDetach 1
 I/System.out: onCreateView 2
 I/System.out: onResume 2

-Reclick tab 1:

 I/System.out: onAttach 1
 I/System.out: onPause 2
 I/System.out: onDetach 2
 I/System.out: onCreateView 1
 I/System.out: onResume 1

when reclick a tabs all fragment lifecyle run again therefor my code load backend run again. How I can keep something like fragment onPause when orther tabs click and OnResume when it click again?.

4 Answers4

0

You need to add this lines in your fragment inside OnCreateView()

Lines to add

 // check if view is null to avoid recreating
 if(rootView != null) {
     return rootView;
 }else {
     rootView = inflater.inflate(R.layout.fragment_film,container,false);
 }
dune98
  • 167
  • 1
  • 11
0

Oh man, I've just been on your place. What I did, I've replaced BottomNavigationView with extended ViewPager, which has turned off swipe gestures. You can find it here: How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?

ViewPager has setOffScreenPageLimit(numberOfPages) method, which will keep desired amount of fragments from sides of screen, to do not redo all hard work.

Also, use simple TabLayout to view tabs at bottom.

Community
  • 1
  • 1
Explosive68
  • 1
  • 1
  • 4
0

Your newInstance() methods probably return new instances every time that they are called. Rewrite your newInstance() methods (Maybe rename it to getInstance, it really should not create new instance every time) using singleton.

Example:

class SomeClass extends Fragment {
    private static SomeClass instance = new SomeClass();
    public static SomeClass getInstance() {
        return instance;
    }
}
0

For BottomNavigationView we need to set one No-Op on reselected listener using BottomNavigationView#setOnNavigationItemReselectedListener. Hope this will solve your problem.

BottomNavigationView navView = findViewById(R.id.navigation);
navView.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
        @Override
        public void onNavigationItemReselected(@NonNull MenuItem item) {
             //Do nothing here.
        }
    });
shobhan
  • 1,460
  • 2
  • 14
  • 28