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.
There are 3-4 ways which I believe can solve your problem.
BottomNavigationView
in XML of MainActivity
and then there are few steps to remember:2.a You need a separate menu XML file containing different navigations of Bottom Navigation, which I believe you might be doing.
2.b. You will need another navigation file to define the navigation tree.
2.c. Now all that is done you can navigate using BottomNavigation and Navigation. Navigating using fragments
and findNavController
gives another advantage of ```sharedViewModels`` and also makes sharing data between different fragments easier if you use navigation and lifecycle libraries.
BottomNavigationView
in every Activity
making it a more tedious process.Personal opinion go for compose
, it will make your life easier, especially with UI and you can spend more time dealing with data. Obviously here again you will have to deal with navigation but the time taken to make a complete UI with navigation drastically reduces when compared to XML-based code.