At current implementation of my app I am using the Navigation component. In my main content I have a floating action button for all. And at the same time I have 7 different fragment which are automatically handled by the navigation controller when transition is needed.
mAppBarConfiguration =
new AppBarConfiguration.Builder( navigationDrawerFragmentIds ).setDrawerLayout( drawer ).build();
NavController navController = Navigation.findNavController( this, R.id.nav_host_fragment );
Till today I used the setUserVisibleHint(boolean isVisibleToUser) method to override different behaviour(on click listener) for my single fab in the each different fragment.
Like below
// FIXME: 2.11.2019 Fix deprecated methods.
@Override public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint( isVisibleToUser );
if (isVisibleToUser && isResumed()) {
//Only manually call onResume if fragment is already visible
//Otherwise allow natural fragment lifecycle to call onResume
onResume();
} else {
// current fragment not visible
floatingActionButton.setOnClickListener( null );
}
}
@Override public void onResume() {
super.onResume();
// FIXME: 2.11.2019 Fix deprecated methods.
if (!getUserVisibleHint()) {
return;
}
// Set listener for float action button which has been defined in main activity.
// Here we will override the listener which can be work for our current fragment.
floatingActionButton = getActivity().findViewById( R.id.fab );
floatingActionButton.setOnClickListener( new View.OnClickListener() {
@Override public void onClick(View view) {
// do some stuff here.
}
} );
}
Since setUserVisibleHint is deprecated anymore, I wanted to replace new behaviour instead this method like suggested in the release note.
Max Lifecycle: You can now set a max Lifecycle state for a Fragment by calling setMaxLifecycle() on a FragmentTransaction. This replaces the now deprecated setUserVisibleHint(). FragmentPagerAdapter and FragmentStatePagerAdapter have a new constructor that allows you to switch to the new behavior.
And also navigation controller has default FragmentNavigator as I understood but I could not understand is this help to me to set the fragments life cycle or behaviour like this post. If I can set the behaviour like mentioned post then I can basically use the onResume and onStart (I assume that will work like this because when I select the BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT then the next fragment will not be initiated. only onCreateView called I assume.)
As I explained above that I am using navigation component to handle transition between my fragments. That is why I do not have any special view pager. But I can still get the information via below code the current destination and callback will handle whenever transition is occured between fragments.
navController.addOnDestinationChangedListener( new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination,
@Nullable Bundle arguments) {
Log.i( TAG, "onDestinationChanged: " + destination.getLabel());
}
} );
Then on my main activity I can set the different behaviour for my fab for each fragment. Up to now this is okey for me. But I wanted to learn is there any way that I can set the max life cycle of the fragment while I am using the Navigation component ?
Thanks in advance.