0

I am having a bottom navigation fragment and want to call API only once, which means when the user jumps to another tab and comes back API should not be getting called. And when the user comebacks from the background state, API should be called once. I tried calling API with the onCreate method, but oncreate method calling every time when I click on the tab. On the other side, for one fragment I am sending data from MainActivity by calling the API.

Please check the code and shed some light on where am doing wrong.

 studentBottomNavView.setOnNavigationItemSelectedListener
            (new BottomNavigationView.OnNavigationItemSelectedListener() {

                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment = null;
                    Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.stdcontainer);
                    switch (item.getItemId()) {
                        case R.id.navigation_std_current_course:

                            if (!(currentFragment instanceof StdCurrentCourse)) {
                                selectedFragment = StdCurrentCourse.newInstance();
                            }
                            isHome = false;
                            break;
                        case R.id.navigation_std_points:
                            if (currentFragment == null) {
                                if (!(currentFragment instanceof StdPoints)) {
                                    selectedFragment = StdPoints.newInstance();
                                }
                            }


                            isHome = false;
                            break;
                        case R.id.navigation_std_home:
                            if (!(currentFragment instanceof StdHome)) {
                           selectedFragment = new StdHome(attendanceList, studentAbsenceAndDelays, todayTaskList);
                           }

                            isHome = true;
                            break;

                        case R.id.navigation_std_task_hw:
                            if (!(currentFragment instanceof StdTaskAndHw)) {
                                selectedFragment = StdTaskAndHw.newInstance();
                            }

                            isHome = false;
                            break;
                        case R.id.navigation_chat:
                            if (!(currentFragment instanceof StdRealTimeChat)) {
                                selectedFragment = StdRealTimeChat.newInstance();
                            }
                            isHome = false;
                            break;
                    }
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.frame_layout1, selectedFragment);
                    transaction.commit();
                    return true;
                }
            });
Mac_Play
  • 302
  • 4
  • 21

1 Answers1

0

You can control API Calls using a Boolean, set as false once you have received a successful response and when BaseActivity/Fragent where studentBottomNavView is called onPause set the same Boolean as true as per your requirement.

Rajiv Reddy
  • 339
  • 1
  • 11
  • but from my understanding the usage of you studentBottomNavView.setOnNavigationItemSelectedListener to set a fragment is not a good practice. – Rajiv Reddy Jan 16 '21 at 10:41
  • I have tried with the boolean in the fragment, still when I comeback API is getting called. – Mac_Play Jan 16 '21 at 11:53