I'm using a navigation graph in my Android app (Java) and have set up a NavController & AppBarConfiguration in my home activity. This is how I have it set up:
public class Activity extends BaseActivity {
NavController navController;
private AppBarConfiguration appBarConfiguration;
private ActivityBinding activityBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
activityBinding = ActivityBinding.inflate(getLayoutInflater());
View view = activityBinding.getRoot();
setContentView(view);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navController = Navigation.findNavController(Activity.this, R.id.fragment);
appBarConfiguration =
new AppBarConfiguration.Builder(navController.getGraph()).build();
setupActionBar(navController, appBarConfiguration);
activityBinding.bottomBar.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.mainFragment:
navController.navigate(R.id.mainFragment);
return true;
case R.id.secondFragment:
navController.navigate(R.id.secondFragment);
return true;
case R.id.thirdFragment:
navController.navigate(R.id.thirdFragment);
return true;
}
return false;
}
});
}
private void setupActionBar(NavController navController,
AppBarConfiguration appBarConfig) {
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfig);
}
@Override
public boolean onSupportNavigateUp() {
return NavigationUI.navigateUp(Navigation.findNavController(this, R.id.fragment), appBarConfiguration);
}
}
I want the NavController & AppBarConfiguration to be hidden to allow for fullscreen visibility in one fragment & one activity. For my fragment, I've tried this solution:
@Override
public void onResume() {
super.onResume();
ActionBar supportActionBar = ((AppCompatActivity) requireActivity()).getSupportActionBar();
if (supportActionBar != null)
supportActionBar.hide();
}
@Override
public void onStop() {
super.onStop();
ActionBar supportActionBar = ((AppCompatActivity) requireActivity()).getSupportActionBar();
if (supportActionBar != null)
supportActionBar.show();
}
And this solution:
getActivity().getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
And this solution:
getActivity().getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
None of these have worked. Does anyone know how to properly show a full screen when using a NavController & AppBarConfiguration?
Solution: As suggested, I added this to my home activity:
public void setBottombarVisibility(int visibility){
activityBinding.bottomBar.setVisibility(visibility);
}
And I have the Fragment where I want the nav to disappear extend this BaseFragment:
public class BaseFragment extends Fragment {
protected int isBottomBarVisible = View.GONE;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Activity act = getActivity();
if (act instanceof Activity) ((Activity) act).setBottombarVisibility(isBottomBarVisible);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
Nav is gone.
Recently, I came across same situation for fab and toolbar. I created a BaseFragment () class and I let all my fragments extended from this. In BaseFragment, declared two variables one for fab's visibility and other for toolbar.
BaseFragment () {
protected int isBottombarVisible =
View.Visible;
//inside oncreateView
Activity activity =
getRequireActivity() //whatever
//the java method to get activity
//inside fragment
activity
.setBottomBarVisibility
(isBottombarVisible)
//define this method in your main
// activity class
}
Inside MainActivity (wherever fragment reside)
public setBottombarVisibility(int
visibility) {
binding.toolbar = visibility
}
Now, inside fragment which was extended from BaseFragment () , override above variable
class Loginfragment() extends
BaseFragment() {
override int isBottombarVisible =
View.Gone
}
I tried to type above code as much as possible in java.. For kotlin refer to link https://github.com/vivekgupta4Git/TacklingNephrotic