My task now is have active bottom navigation view and toolbar which has buttons when first of buttons open new fragment and second which return to initial fragment.
i had base fragmentlayout and bottom navigation view. I found way change fragment without bottom navigation view with supportFragmentManager. But after replace() my bottom navigation view stops work. How can i fix this?
if you didnt understand i attach photos
tool bar buttons opens and returns fragments without bottom navigation view
Now my main activity look like
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
val navHostFragment = supportFragmentManager.findFragmentById(binding.fragmentContainerView.id)
as NavHostFragment
binding.bnvBase.setupWithNavController(navHostFragment.navController)
binding.toolbar.ivBack.setOnClickListener {
supportFragmentManager.commit {
replace(R.id.fragmentContainerView, PokemonCardsFragment())
setReorderingAllowed(true)
addToBackStack(null)
}
}
binding.toolbar.searchButton.setOnClickListener {
supportFragmentManager.commit {
replace(R.id.fragmentContainerView, SearchPokemonCardsFragment(
binding.toolbar.searchSrcText.text.toString()
))
setReorderingAllowed(true)
addToBackStack(null)
}
}
}
Related
I want to implement a single activity project.
After showing splash i want to navigate to HomeFragment or LoginFragment based on user's login condition. In the MainActivity :
override fun onCreate(savedInstanceState: Bundle?) {
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (userLoggedIn()) {
navigateToHomeScreen()
} else {
navigateToLoginScreen()
}
}
The question is how should i set the navigation xml? Which one of the fragments should be the startDestionation. What's the best approach for a smooth splash/login( sign-in/sign-up/forget-pass)/ home flow.
I have a Home fragment with multiple buttons and when I click the Contact button, another fragment is opened. Inside this fragment I have two child fragments and two buttons, and I can switch between those child fragments using those buttons. The problem is when I press the Back Button, it switches back between child fragments and only after that it goes back to Home fragment, but I want to directly go back to Home Fragment.
This is how I'm opening the child fragments:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val contactsListFragment = ContactsListFragment()
val groupsListFragment = GroupsListFragment()
activity?.title = getString(R.string.contacts_and_groups)
openChildFragment(contactsListFragment)
binding.contactsButton.setOnClickListener {
openChildFragment(contactsListFragment)
}
binding.groupsButton.setOnClickListener {
openChildFragment(groupsListFragment)
}
}
private fun openChildFragment(fragment: Fragment) {
val childFragmentManager = childFragmentManager
val transaction: FragmentTransaction = childFragmentManager.beginTransaction()
transaction.replace(binding.contactsGroupsFl.id, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
If anyone can help me with this issue would be great. Thanks!
I think you should add a listener for the back button in both of your fragments so that you can clear all the backstack when it's pressed. Something like this :
requireActivity().onBackPressedDispatcher.addCallback(
viewLifecycleOwner,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
for (i: Int in 1..parentFragmentManager.backStackEntryCount) {
parentFragmentManager.popBackStack()
}
}
})
I am using TabLayout with ViewPager. I have 3 fragments and 3 tabs. I want to access the FloatingActionButton in activitymain.xml file.
In every fragment I write a code like this:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val fab :View= (requireActivity() as MainActivity).findViewById(R.id.fab_main)
fab.setOnClickListener {
Toast.makeText(context,"Fragment1",Toast.LENGTH_SHORT).show()
}
}
But when I click the FloatingActionButton in every fragment it gives me the same Toast message which is related to the last fragment. I want to perform different actions in every fragment when I clicked the button.
You could use FragmentStatePagerAdapter for tab function
class TabAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
..........
}
It will be make Fragment call onResume when the fragment is active.
So you can like this in Fragment
override fun onResume() {
super.onResume()
val fab :View= (requireActivity() as MainActivity).findViewById(R.id.fab_main)
fab.setOnClickListener {
Toast.makeText(context,"Fragment1",Toast.LENGTH_SHORT).show()
}
}
Let me add more context:
I run bottom view navigation with a ViewPager2. For all 4 of my tab/fragments of my bottom navigation view I have an options menu which I created separately dynamically in each fragment.
Now, when we navigate through the app, it behaves correctly as every options menu is displayed ONLY for their respective fragment.
Problem is: Only when the app is launched, all the options menus from all the the 4 fragments show up on the start destination fragment's tab. BUT, once we swipe and swipe back, only the start destinations options menu is shown on the app bar. As is for every of the other 3 fragment/tabs.
Theory: I think it has something to do with onCreateOptionsMenu which is called when all four fragments are also created to which they share an app bar.
Is anybody familiar with this type of issue? Here is my PagerAdapters code for my ViewPager:
const val F1_PAGE_INDEX = 0
const val F2_PAGE_INDEX = 1
const val F3_PAGE_INDEX = 2
const val F4_PAGE_INDEX = 3
class PagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
/**
* Mapping of the ViewPager page indexes to their respective Fragments
*/
private val tabFragmentsCreators: Map<Int, () -> Fragment> = mapOf(
F1_PAGE_INDEX to { FirstFragment() },
F2_PAGE_INDEX to { SecondFragment() },
F3_PAGE_INDEX to { ThirdFragment() },
F4_PAGE_INDEX to { FourthFragment() }
)
override fun getItemCount() = tabFragmentsCreators.size
override fun createFragment(position: Int): Fragment {
return tabFragmentsCreators[position]?.invoke() ?: throw IndexOutOfBoundsException()
}
}
Here is also my Home View Pager Fragment where I create my bottom Navigation and affect to to the main fragments:
class HomeViewPagerFragment(): Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = FragmentViewPagerBinding.inflate(inflater, container, false)
val viewPager = binding.viewPager
viewPager.isUserInputEnabled = false
viewPager.adapter = PagerAdapter(this)
//Save states of four fragments
viewPager.offscreenPageLimit = 4
(activity as AppCompatActivity).setSupportActionBar(binding.toolbar)
val bottomNavigation = binding.bottomNavView
bottomNavigation.setOnNavigationItemSelectedListener(
BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.fragment1_destination -> viewPager.currentItem = F1_PAGE_INDEX
R.id.fragment2_destination -> viewPager.currentItem = F2_PAGE_INDEX
R.id.fragment3_destination -> viewPager.currentItem = F3_PAGE_INDEX
R.id.fragment4_destination -> viewPager.currentItem = F4_PAGE_INDEX
}
true
})
return binding.root
}
}
The starting destination (first fragment) is where all the fragment's menu are shown at app start.
Any kind of help is appreciated! Thank you!
Issue: After endless hours, we have found a solution. The issue was directly created when we set the off screen page limit to 4. That causes all the fragments to be created at the same time, thus, obligates the option menus to be shown at launch on the starting destinations fragment since we instructed setHasOptionsMenu(true) in the onCreate of each fragment.
Solution: Simply, set the options menu to true in the onResumeof the fragment to only be called when we swipe to the respective fragment in this manner:
override fun onResume() {
super.onResume()
setHasOptionsMenu(true)
}
I think what you need to do is, make OptionsMenu visible only when that fragment is visible.
Try this: Put this in each fragment
override fun onResume(){
super.onResume()
setHasOptionsMenu(isVisible())
}
This will make the options menu visible only when that fragment is visible. You can make it hidden in onCreateView or in onPause if just onResume doesn't work.
I'm trying to create a single activity Android application.
I have MainActivity (only activity) with BottomNavigationView, three top level fragments and some child fragments. My requirement is whenever the screen is showing top level fragments, bottom navigation should be visible such that switching is possible. But when I'm viewing any of the child fragments, bottom navigation should be hidden.
Is there any out-of-box way using the Navigation component or need to change the visibility manually ?
Update (Navigation component 1.0)
As of Navigation component 1.0.0-alpha08, method addOnNavigatedListener(controller: NavController, destination: NavDestination) was changed to addOnDestinationChangedListener(controller: NavController, destination: NavDestination, arguments: Bundle). Its behavior was also slightly changed (it is also called if the destinations arguments change).
Old Answer
You can use NavController.OnNavigatedListener to achieve this behavior (set it in Activity onCreate):
findNavController(R.id.container).addOnNavigatedListener { _, destination ->
when (destination.id) {
R.id.dashboardFragment -> showBottomNavigation()
else -> hideBottomNavigation()
}
}
private fun hideBottomNavigation() {
// bottom_navigation is BottomNavigationView
with(bottom_navigation) {
if (visibility == View.VISIBLE && alpha == 1f) {
animate()
.alpha(0f)
.withEndAction { visibility = View.GONE }
.duration = EXIT_DURATION
}
}
}
private fun showBottomNavigation() {
// bottom_navigation is BottomNavigationView
with(bottom_navigation) {
visibility = View.VISIBLE
animate()
.alpha(1f)
.duration = ENTER_DURATION
}
}
Using addOnDestinationChangedListener works, and it's the solution recommended in the official documentation, but it does cause some flickering, as the callback is executed before the fragment is attached.
I find the below answer more flexible, and handles animations better:
supportFragmentManager.registerFragmentLifecycleCallbacks(object : FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentViewCreated(fm: FragmentManager, f: Fragment, v: View, savedInstanceState: Bundle?) {
TransitionManager.beginDelayedTransition(binding.root, Slide(Gravity.BOTTOM).excludeTarget(R.id.nav_host_fragment, true))
when (f) {
is ModalFragment -> {
binding.bottomNavigation.visibility = View.GONE
}
else -> {
binding.bottomNavigation.visibility = View.VISIBLE
}
}
}
}, true)
You can customize it depending on the transitions between your fragments, by choosing different animation (on my example it's a Slide), or by making the call at another lifecycle callback.
You have to make a method in MainActivity for visibility. Do call that method from fragments where you want to show or hide.
One thing I faced with such scenario is, bottom navigation visibility is not being properly gone. So I put bottom navigation view in Relative layout and hide that parent view.
you just need to write this code in MainActivity
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Getting the Navigation Controller
navController = Navigation.findNavController(this, R.id.fragment)
//Setting the navigation controller to Bottom Nav
bottomNav.setupWithNavController(navController)
//Setting up the action bar
NavigationUI.setupActionBarWithNavController(this, navController)
//setting the Bottom navigation visibiliy
navController.addOnDestinationChangedListener { _, destination, _ ->
if(destination.id == R.id.full_screen_destination ){
bottomNav.visibility = View.GONE
}else{
bottomNav.visibility = View.VISIBLE
}
}
}
get more details from the android developer documentation:
Update UI components with NavigationUI
So even tho this question was already answered and the accepted answer is one that works, here is the code to actually achieve this behaviour:
MainActivity
fun hideBottomNav() {
bottomNavigationView.visibility = View.GONE
}
fun showBottomNav() {
bottomNavigationView.visibility = View.VISIBLE
}
Then you call the functions in your fragment onViewCreated(), onDetach() function, like:
Fragment
class FragmentWithOutBottomNav() : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
(activity as MainActivity).hideBottomNav()
}
override fun onDetach() {
super.onDetach()
(activity as MainActivity).showBottomNav()
}
}
Hope I could help some people. Happy coding!
navController.addOnDestinationChangedListener { _, destination, _ ->
val isMainPage = bottomNavigationView.selectedItemId == destination.id
bottomNavigationView.isVisible = isMainPage
}