I was developing an app that became quite big in code when I noticed this problem, so I made a test app that contains a bottom navigation bar with main activity and three fragments (F1,F2,F3) and set it up with navigation component. I logged the life-cycle events of the first fragment like below and this is what I got:
public class F1 extends Fragment {
private OnFragmentInteractionListener mListener;
private static final String TAG = "F1";
public F1() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate: ");
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "onActivityCreated: ");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
@Override
public void onDetach() {
super.onDetach();
Log.d(TAG, "onDetach: ");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume: ");
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop: ");
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart: ");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Log.d(TAG, "onCreateView: ");
return inflater.inflate(R.layout.fragment_f1, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, "onViewCreated: ");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG, "onDestroyView: ");
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
When navigating from f1 to f2 the logcat:
2019-09-22 14:11:34.648 6580-6580/com.example.navtest D/F1: onStop:
2019-09-22 14:11:34.648 6580-6580/com.example.navtest D/F1: onDestroyView:
When navigating from f2 back to f1 using the nav bar:
2019-09-22 14:12:41.406 6580-6580/com.example.navtest D/F1: onCreate:
2019-09-22 14:12:41.406 6580-6580/com.example.navtest D/F1: onCreateView:
2019-09-22 14:12:41.408 6580-6580/com.example.navtest D/F1: onViewCreated:
2019-09-22 14:12:41.408 6580-6580/com.example.navtest D/F1: onActivityCreated:
2019-09-22 14:12:41.408 6580-6580/com.example.navtest D/F1: onStart:
2019-09-22 14:12:41.408 6580-6580/com.example.navtest D/F1: onResume:
2019-09-22 14:12:41.410 6580-6580/com.example.navtest D/F1: onDestroy:
2019-09-22 14:12:41.410 6580-6580/com.example.navtest D/F1: onDetach:
Can anybody explain this behavior please? The app works normal and the fragment is visible and functions normally so why is onDestory/onDetach being called? Could it be a bug or what? I've had it in both projects and the common thing between them is how i Set up the bottom app bar in the main activity which is the normal way but I'll post it anyway:
NavController navController = Navigation.findNavController(this, R.id.navHostFragment);
BottomNavigationView appBar = findViewById(R.id.appBar);
NavigationUI.setupWithNavController(appBar, navController);
EDIT: Fragment one tested above was the start destination, logging fragment two every thing seems normal, it's only when I assign it (fragment 2) as the start destination fragment one acts as expected and fragment two shows the weird behavior. I think the issue is specifically about the starting destination fragment.