BottomNavigationViw code
1 2 3 4 5 6 7 |
|
1. @Override indicates that you are overriding the onBackPressed method from the parent class (presumably
AppCompatActivity or similar) to customize the back button behavior.
2. Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frament_
container_admin);: This line retrieves the currently displayed fragment within the
fragment_container_admin container using the FragmentManager. It assumes that the
fragments are being replaced within this container.
3. BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav_admin);:
This line retrieves a BottomNavigationView from your layout with the ID bottom_nav_admin.
You are going to use this view to change the selected item.
4. The code then checks which fragment is currently being displayed by using the
instanceof operator. If the current fragment is an instance of AdminSettingFragment,
it sets the selected item in the BottomNavigationView to R.id.action_Admin_View.
If it's an instance of AdminScheduleFragment, it sets the selected item to R.id.action_Admin_Setting.
5. If the current fragment is neither of the above types, it calls super.onBackPressed(), which invokes the default behavior for the back button. This will typically lead to the activity being popped from the back stack or, if there's no more back stack, closing the activity.
So, in summary, this code is designed to customize the behavior
of the back button based on the currently displayed fragment. If it's a
specific fragment, it changes the selected item in the BottomNavigationView, and if it's any other
fragment, it performs the default back button behavior.
1 2 3 4 5 6 7 |
|
Drawer code
1 2 3 4 5 6 7 |
|
Here's an explanation of what this code does:
onBackPressedis an overridden method that handles the back button press in your Android activity.It first checks if the navigation drawer (identified by
R.id.drawer_layout) is open usingdrawer.isDrawerOpen(GravityCompat.START). If the drawer is open, it closes it to allow users to navigate away from the drawer.If the navigation drawer is closed, the code proceeds to handle the back press. It retrieves the currently displayed fragment using
getSupportFragmentManager().findFragmentById(R.id.frame_layout). This assumes that you have a container (e.g., aFrameLayout) with the IDframe_layoutwhere you replace your fragments.Depending on the type of fragment you have, it performs different actions:
- If the current fragment is an instance of
AddNewMumberFragment, it loads a new fragment,UserDetailsShowFragment, using theloadFragmentmethod. - If the current fragment is an instance of
UpdateUserFragment, it also loads theUserDetailsShowFragment. - If the current fragment is neither of the above types, it calls
super.onBackPressed(), which invokes the default behavior for the back button. This typically leads to the activity being popped from the back stack or, if there's no more back stack, closing the activity.
- If the current fragment is an instance of
The
loadFragmentmethod is used to replace the current fragment with a new fragment within theframe_layoutcontainer.
In summary, this code handles the back button press by checking the current fragment and taking appropriate actions. If it's one of the specified fragments, it replaces it with a different fragment; otherwise, it performs the default back button behavior.
1 2 3 4 5 6 7 |
|