Reputation: 365
I use bottom navigation bar (xx_activity bottom by default) but I have problems. When I click on the item it's OK, the activity is good but the item of the activity not change...
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.BottomNavigationView
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_grammaire.*
class GrammaireActivity : AppCompatActivity() {
private val mOnNavigationItemSelectedListener=BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_grammaire -> {
val intent = Intent(this,GrammaireActivity::class.java)
startActivity(intent)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_situations -> {
val intent = Intent(this,SituationsActivity::class.java)
startActivity(intent)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_lexiquefrsa -> {
val intent = Intent(this,LexiqueFrSaActivity::class.java)
startActivity(intent)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_lexiquesafr -> {
val intent = Intent(this,LexiqueSaFrActivity::class.java)
startActivity(intent)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_infos -> {
val intent = Intent(this,InfosActivity::class.java)
startActivity(intent)
return@OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_grammaire)
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
}
}
Upvotes: 2
Views: 5391
Reputation: 763
I am not sure about "item of the activity not change" but i think you mean to say that the content of your current Activity doesn't change.
So for that you should replace Fragments in an Activity on the click of the BottomNavigation item. This is how it should be done
when (item.itemId) {
R.id.navigation_grammaire -> {
supportFragmentManager.beginTransaction.replace(R.id.container, FragmnetGrammaire().commit())
}
where "container" is the id of the view above your BottomNavigation bar. It can be FrameLayout. FragmnetGrammaire(), is the instance of your Fragment.
Upvotes: 1
Reputation: 316
I believe it's recommended to use Fragments with bottom navigation, rather than Activities. With the onClick you would swap out the current Fragment with the one identified by the click.
Upvotes: 1