BottomNavigationView not selecting item on fragment backstack - android

I have a small app with on activity and two fragments inside. The fragments are loaded with the BottomNavitationView.
MonitoringFragment gets loaded on the OnCreate in the activity.
I want to add this one to the backstack so when I'm inside the second fragment (ConnectionFragment) and i press back I get to the first fragment. This works fine. However the BotttonNavigationView doesn't get updated (doesn't set the first item as selected when returning from second fragment. picture 3). I assume it doesn't handle this behavior by itself and I have to implement it myself but every attempt I made was unsuccessfull.
Activity code:
Fragment activeFragment = null;
BottomNavigationView bottomNavigationView = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
switch (item.getItemId()) {
case R.id.monitoring:
setCurrentFragment(new MonitoringFragment(), false);
break;
case R.id.connection:
setCurrentFragment(new ConnectionFragment(), true);
break;
}
return true;
});
setCurrentFragment(new MonitoringFragment(), true);
}
private void setCurrentFragment(Fragment fragment, boolean addToBackStack) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
if (addToBackStack) {
fragmentTransaction.addToBackStack(null);
}
fragmentTransaction.commit();
activeFragment = fragment;
}
Thanks!

Yes, BottomNavigationView is not setup with fragmentManager. You might set prop select tab by yourself, when a fragment gets resumed.
Or you can use navigation component with BottomNavigationView, those working ok together.
With the second approach when pressing back button will not return you from 2nd fragment to 1st one.

Faced a similar problem, here is how I solved it
I added OnDestinationChangedListener to my navcontroller, and created several arraylists with destination label of fragments in my progect.
When current destination is in one of these fragments, bottom menu button is checked
I hope the code example is clearer than my explanation)
val destListNews = arrayListOf(
"news_fragment",
"NewsViewingFragment"
)
val destListAds = arrayListOf(
"ads_view_fragment",
"ads_fragment",
"AdsAddFragment"
)
val destListPass = arrayListOf(
"pass_fragment",
"pass_creation_fragment",
"PassViewFragment"
)
val destListVotes = arrayListOf(
"VotesListFragment",
"VotesAddFragment",
"ChooseVoteTypeFragment"
)
navController.addOnDestinationChangedListener { controller, destination, arguments ->
when (destination.label) {
in destListNews -> {
bottom_nav.menu.getItem(0).isChecked = true
}
in destListAds -> {
bottom_nav.menu.getItem(1).isChecked = true
}
in destListPass -> {
bottom_nav.menu.getItem(2).isChecked = true
}
in destListVotes -> {
bottom_nav.menu.getItem(3).isChecked = true
}
else -> {
bottom_nav.menu.getItem(4).isChecked = true
}
}
}

Related

Managing fragments with bottom navigation bar

I face problem with changing fragment in my container. I have three navigation: Home, Special offers, Profile. In Home navigation it could be fragment1_1 or fragment2_2 depending on situation. My problem is getting showed fragment from my container. I try to get using findFragmentById, but when I in Profile navigation and try to go to Home my code do not hide() fragment from Pofile. I tried to see the logs and I see that it hides Home and shows Home. My code for navigation:
botNav.setOnNavigationItemSelectedListener {
when(it.itemId){
R.id.act_home -> {
if (!it.isChecked){
val homeFragment = supportFragmentManager.findFragmentByTag("Home")
activeFragment = supportFragmentManager.findFragmentById(R.id.fragment_container)
activeFragment?.let { hideFragment(it) }
showFragment(homeFragment!!)
}
}
R.id.act_profile_info -> {
if (!it.isChecked) {
activeFragment = supportFragmentManager.findFragmentById(R.id.fragment_container)
activeFragment?.let { hideFragment(it) }
showFragment(profileFragment)
}
}
R.id.act_special_offer -> {
if (!it.isChecked) {
activeFragment = supportFragmentManager.findFragmentById(R.id.fragment_container)
activeFragment?.let { hideFragment(it) }
showFragment(specialFragment)
}
}
}
return#setOnNavigationItemSelectedListener true
}
So now I want to understand why it is acting so and how can I get shown fragment from FrameLayout container.
For adding fragments for my navigation I used addFragment() function.
You can try this. Working fine for me tested.
Call this bellow method whenever want to add and show previous existing fragment
/**
* Method for add and replace and set fragment if exist in stack
*/
fun setAndReplaceFragment(fragmentWantToAdd: Fragment, tag: String) {
val manager = supportFragmentManager
val fragmentFind = manager.findFragmentByTag(tag)
if (fragmentFind != null) {
val ft = manager.beginTransaction()
ft.replace(R.id.mFrmContainer, fragmentFind, tag)
ft.addToBackStack(tag)
ft.commit()
} else {
val ft = manager.beginTransaction()
ft.replace(R.id.mFrmContainer, fragmentWantToAdd, tag)
ft.addToBackStack(tag)
ft.commit()
}
}
you can try this one :
//Fragment1 is your new fragment to be shown.
Fragment fragment=new Fragment1();
if (fragment != null) {
FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
//frams is your backup fragment upon on your navigation/new black
fragment(thi will beshown if the Fragment1 is not working)
ft.replace(R.id.frams, fragment);
ft.commit();

Multiple Fragments in each page of a BottomNavigationView

I use a BottomNavigationView with four pages. If I want to add 4 fragments to that it is Ok but when I want to replace new fragment with old fragment in one of the BottomNavigationView page and restore it when item click, the first fragment open again. How can I restore the last fragment? Should I use different FrameLayout in the first BottomNavigationView?
Try this:
bottomNavView.setOnNavigationItemSelectedListener(object : BottomNavigationView.OnNavigationItemSelectedListener {
override fun onNavigationItemSelected(item: MenuItem): Boolean {
clearAllBackStack()
when (item.itemId) {
R.id.itemHome -> {
// load fragment here
}
R.id.itemMyAccount -> {
// load fragment here
}
R.id.itemSettings -> {
// load fragment here
}
R.id.itemNotfications -> {
// load fragment here
}
}
return true
}
})
add this method:
fun clearAllBackStack() {
for (i in 0 until supportFragmentManager.backStackEntryCount) {
supportFragmentManager.popBackStack()
}
}
Note: also add fragment to backstack whenever you load fragment, add this when you load fragment: fragmentTransaction.addToBackStack(null)

Fragments Resume: Show and hide transactions don't work

I want to resume android fragments that are in the backstack. When switching between different tabs on bottomnavigation,i don't want the views of fragments to be recreated.
I read this, this,this and some other questions on stackoverflow related to this. Common suggestions are to use show and hide method of fragment transactions but it is not working. Here is my kotlin code:
bottomnavigation.setOnNavigationItemSelectedListener {
item ->
when(item.itemId){
R.id.first_fragment_item -> {
var fragment:Fragment = FirstFragment.newInstance()
replaceFragment(fragment)
return#setOnNavigationItemSelectedListener true
}
R.id.second_fragment_item -> {
var fragment:Fragment = SecondFragment.newInstance()
replaceFragment(fragment)
return#setOnNavigationItemSelectedListener true
}
}
return#setOnNavigationItemSelectedListener false
}
}
fun replaceFragment(fragment:Fragment) {
var fragmentName:String = fragment::class.simpleName!!
if(supportFragmentManager.findFragmentByTag(fragmentName)==null) {
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragment_container, fragment, fragmentName)
fragmentTransaction.addToBackStack(fragmentName)
fragmentTransaction.commit()
}
else{
if(fragmentName == "FirstFragment") {
supportFragmentManager.beginTransaction().
hide(supportFragmentManager.findFragmentByTag("SecondFragment"))
.show(supportFragmentManager.findFragmentByTag("FirstFragment"))
.commit()
}
else{
supportFragmentManager.beginTransaction()
.hide(supportFragmentManager.findFragmentByTag("FirstFragment"))
.show(supportFragmentManager.findFragmentByTag("SecondFragment"))
.commit()
}
}
}
The last fragment of backstack is always shown. When i want to hide it and show
the other fragment, that fragment screen becomes white with nothing on it.
My bottomnavigation has four items but for testing purpose i am only using two. Two of fragments are FirstFragment and SecondFragment. I am using v4.app.fragments.
With the help of Kishan Viramgama comment's, i solved my problem. I used "add" method instead of "replace". This is my code for resuming fragments: `
fun replaceFragment(fragment: Fragment) {
var nameOfFragment: String? = fragment::class.simpleName
var fragmentTransaction = supportFragmentManager.beginTransaction()
var fm: FragmentManager = supportFragmentManager
if (supportFragmentManager.findFragmentByTag(nameOfFragment) == null) {
fragmentTransaction.add(R.id.root_fragments, fragment, nameOfFragment) //if fragment is not added to the backstack,add it/ don't use replace because it creates new fragment emptying fragment container
fragmentTransaction.addToBackStack(nameOfFragment)
fragmentTransaction.commit()
} else {
var fragmentToShow:Fragment = supportFragmentManager.findFragmentByTag(nameOfFragment) //if fragment is already in backstack,show it and hide all other fragments.
for (i in 0 until fm.backStackEntryCount) {
var fragmentToHide: Fragment = fm.findFragmentByTag(fm.getBackStackEntryAt(i).name)
if (fragmentToHide::class.simpleName != fragment::class.simpleName)
fragmentTransaction.hide(fragmentToHide)
}
fragmentTransaction.show(fragmentToShow)
fragmentTransaction.commit()
}
}
`

BottomNavigationView lags on fragment transaction

The problem
I'm using the BottomNavigationView from the Android Design Support Library on one of my Activities, alongside with Fragments for each navigation item.
Each time I select an item on the bar, I do a fragment transaction, like the snippet below (some parts of the code was removed for brevity):
private var fragmentToSet: Fragment? = null
private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
fragmentToSet = when (item.itemId) {
// Choose fragment based on selection
// ...
}
// ...
supportFragmentManager.beginTransaction()
.replace(R.id.container, fragmentToSet)
.commit()
}
The problem is... The bottom bar animation gets super laggy, and only finishes after the fragment is fully loaded and displayed on the screen.
This issue is not exactly new since it can also happen while using the Navigation Menu, but at least it's possible to solve it by using the DrawerLayout.DrawerListener and do the actual Fragment transaction only after the drawer is closed.
What I've tried so far
I tried to "cache" the fragments, holding their reference to avoid recreating the objects every time (e.g. MyFragment.newInstance()), but that didn't work.
I also tried to use handlers, which kinda solved the problem, but it might lead me to an exception in some cases. Something like the snippet below:
handler.postDelayed({changeFragment(fragmentToSet!!)}, 200)
Is there a way to solve this issue without using handlers (or other async calls), on a similar fashion to this solution while using the Navigation Menu?
I handled this situation by hiding and showing fragments using fragment manager. I wrote a sample code to deal with it as below.
class MainActivity : BaseActivity() {
private val homeFragment = HomeFragment.newInstance()
private val categoryFragment = CategoryFragment.newInstance()
private val searchFragment = SearchFragment.newInstance()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
navigation.menu.findItem(R.id.navigation_home).isChecked = true
supportFragmentManager.beginTransaction()
.add(R.id.containerFrameLayout, homeFragment)
.add(R.id.containerFrameLayout, categoryFragment)
.add(R.id.containerFrameLayout, searchFragment)
.commit()
setTabStateFragment(TabState.HOME).commit()
}
override fun onBackPressed() {
if (supportFragmentManager.backStackEntryCount > 0 || !homeFragment.isHidden) {
super.onBackPressed()
} else {
setTabStateFragment(TabState.HOME).commit()
navigation.menu.findItem(R.id.navigation_home).isChecked = true
}
}
private fun setTabStateFragment(state: TabState): FragmentTransaction {
supportFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
val transaction = supportFragmentManager.beginTransaction()
transaction.setCustomAnimations(R.anim.fragment_enter, R.anim.fragment_exit)
when (state) {
TabState.HOME -> {
transaction.show(homeFragment)
transaction.hide(categoryFragment)
transaction.hide(searchFragment)
}
TabState.CATEGORY -> {
transaction.hide(homeFragment)
transaction.show(categoryFragment)
transaction.hide(searchFragment)
}
TabState.SEARCH -> {
transaction.hide(homeFragment)
transaction.hide(categoryFragment)
transaction.show(searchFragment)
}
}
return transaction
}
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_home -> {
setTabStateFragment(TabState.HOME).commit()
return#OnNavigationItemSelectedListener true
}
R.id.navigation_category -> {
setTabStateFragment(TabState.CATEGORY).commit()
return#OnNavigationItemSelectedListener true
}
R.id.navigation_search -> {
setTabStateFragment(TabState.SEARCH).commit()
return#OnNavigationItemSelectedListener true
}
}
false
}
internal enum class TabState {
HOME,
CATEGORY,
SEARCH,
}
}

Two Fragments overlapping each other

I am using a BottomNavigationView for a Tab-Interface in my main activity. in onCreate(), the switchTab method is called with the initial fragment. Tapping the the bottom navigation calls switchTab() for the respective tabs and should hide the current and display the new one. If the fragment was not added to the SupportFragmentManager, it gets added, otherwise it will be shown. Here's my code snippet:
private fun switchTab(fragment: Fragment, tag: String): Boolean {
val currentFragment = supportFragmentManager.fragments.find { it.tag == tag }
val ta = supportFragmentManager.beginTransaction()
if (currentFragment != null) {
ta.hide(currentFragment)
}
if (supportFragmentManager.fragments.contains(fragment)) {
ta.show(fragment)
} else {
ta.add(R.id.contentContainer, fragment, tag)
}
ta.commit()
return true
}
Now the problem is that sometimes two fragments are visible and overlay each other, making the userinterface unusable. How can this happen?

Categories

Resources