Several years ago I built an application using a few different Activities to handle different screens throughout my app (I was a complete beginner at Android app development).
As time progressed, so did the magnitude of the app. New features required new activities and logic for each feature/screen, while the app evolved to its current stage. Looking back I realize that perhaps the better way to handle this would have been to use fragments from the start (or not?).
Nonetheless, I now have a fairly large application with roughly 15+ completely distinct and separate activities that each completes a separate feature of the app.
My summer project is going to be to overhaul the design and user interface - it is very outdated -by adding a Bottom Navigation Bar, and a Slider Drawer. However, all of the tutorials and examples I can find online all refer to using Fragments to accomplish this instead of Activities. My problem, is that the current state of the application is built using exclusively Activities.
I thought, inheritance would be able to handle this type of task to incorporate the navigation separately and then incorporating it into the activities by using an open class. On my MainActivity.kt this seems to be working reasonably well
class MainActivity : BottomNavActivity() {
However all other activities that I transition to (through the use of an Intent) do not display the Bottom Navigation Bar, even if they inherit from the BottomNavActivity() class.
open class BottomNavActivity : AppCompatActivity() {
override fun setContentView(layoutResID: Int) {
setContentView(LayoutInflater.from(this).inflate(layoutResID, null, false))
}
override fun setContentView(view: View?) {
super.setContentView(view)
if (view is LinearLayout ) {
view.also {
val bottomNav =
LayoutInflater.from(this).inflate(R.layout.activity_bottom_navigation, null, false)
bottomNav.layoutParams = RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
).apply {
addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
}
it.addView(bottomNav)
}
}
// Program the FAB button
val myFab = findViewById<FloatingActionButton>(R.id.fab)
myFab.setOnClickListener {
// Still need to build logic for opening the slider drawer when the FAB is clicked
}
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
bottomNavigationView.background = null
bottomNavigationView.menu.getItem(2).isEnabled = false
bottomNavigationView.setOnItemSelectedListener {
if (it.itemId == R.id.lines){
var intent = Intent(this, ChooseTeamLineComboActivity::class.java)
// go to the line combinations activity
startActivity(intent)
}
else if(it.itemId == R.id.rankings){
var intent = Intent(this, ChooseRankingTypeActivity::class.java)
// go to choose the view rankings selection activity
startActivity(intent)
}
else if(it.itemId == R.id.search){
var intent = Intent(this, SearchActivity::class.java)
// start the Search activity
startActivity(intent)
}
return@setOnItemSelectedListener true
}
}}
I suppose my question is: Should I convert the entire project from Activities to Fragments? Is it worth the battle?
Or
How can I implement every activity to have the Bottom Navigation Bar using inheritance? Are there things that I will need to worry about (for instance, the transition from one Activity to the next won't look right).
Or
Should I add the Bottom Navigation Bar code directly to every Activity without inheritance? This seems cumbersome and against all general coding practices.
I appreciate any guidance you can give me.