Even when I’ve just realised that action bar back button is not completely necessary in my app (because access to all screens are already present in footer menu), I need to understand why it is making my app unresponsive.
To start with necessary information, I’ll first say that my main activity is “TestTypeMenuActivity” and that all activities inherit from a base activity called “BaseActivity”.
Let’s take one of my activities as an example to work with, CollegeSearchActivity:
In my AndroidManifest I define it and set its parent to main activity as follows:
<activity
android:name=".ui.activities.main.collegesearch.CollegeSearchActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.activities.test.testtypemenu.TestTypeMenuActivity" />
</activity>
In CollegeSearchActivity:
override fun createActionBar() {
val activityTitle = TMLocale.getStringResourceByResId(R.string.activitycommunity_textview_findcolleges).uppercase()
super.createActionBar(activityTitle, showBackButton = true, capitalizeTitle = true)
}
In BaseActivity:
fun createActionBar(title: String, showBackButton: Boolean, capitalizeTitle: Boolean){
if(useToolBar){
val toolbar = this.createToolbar(title, capitalizeTitle)
if (toolbar!=null) setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(showBackButton)
val toolbarColor = getActivityTextColor()
toolbar?.setNavigationIconColor(Color.parseColor(toolbarColor))
}
}
Extension Methods class:
fun Activity.createToolbar(actionbarTitle: String, capitalizeTitle: Boolean = true): Toolbar? {
val toolbar = findViewById<Toolbar>(R.id.toolbar)
if (toolbar != null) {
this.setActionBarBackground(toolbar)
val capitalizedTitle = if (capitalizeTitle)
WordUtils.capitalizeFully(actionbarTitle) else actionbarTitle
val tvActionBarTitle = toolbar.findViewById<TextView>(R.id.tvActionBarTitle)
val textColor = BaseActivity.getAppTextColor(context)
tvActionBarTitle.setTextColor(Color.parseColor(textColor))
if (tvActionBarTitle != null) tvActionBarTitle.text = capitalizedTitle
}
return toolbar
}
Everything was working fine until now, when suddenly back button in action bar started making my app unresponsive, and I cannot figure out why.
The weird thing is that if I insert the next lines -taken from another SO related question- into my BaseActivity the problem just disappear:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
super.onBackPressed()
return true
}
return super.onOptionsItemSelected(item)
}
Instead of being “lazy” and just forget about the issue when it is solved just putting that lines below, I need to understand why the problem started happening and why that lines solves it.
Any help?