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;
}
});