Make back button close menu and return to home
This commit is contained in:
parent
900c5db0dc
commit
c3e3444b07
|
@ -21,6 +21,7 @@ import android.widget.TextView
|
||||||
import androidx.appcompat.app.ActionBarDrawerToggle
|
import androidx.appcompat.app.ActionBarDrawerToggle
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.appcompat.widget.Toolbar
|
import androidx.appcompat.widget.Toolbar
|
||||||
|
import androidx.core.view.GravityCompat
|
||||||
import androidx.viewpager2.widget.ViewPager2
|
import androidx.viewpager2.widget.ViewPager2
|
||||||
import com.google.android.material.tabs.TabLayoutMediator
|
import com.google.android.material.tabs.TabLayoutMediator
|
||||||
import com.google.firebase.crashlytics.FirebaseCrashlytics
|
import com.google.firebase.crashlytics.FirebaseCrashlytics
|
||||||
|
@ -87,6 +88,7 @@ class MainActivity : NoSplashAppCompatActivity() {
|
||||||
private lateinit var actionBarDrawerToggle: ActionBarDrawerToggle
|
private lateinit var actionBarDrawerToggle: ActionBarDrawerToggle
|
||||||
private var pluginPreferencesMenuItem: MenuItem? = null
|
private var pluginPreferencesMenuItem: MenuItem? = null
|
||||||
private var menu: Menu? = null
|
private var menu: Menu? = null
|
||||||
|
private var menuOpen = false
|
||||||
|
|
||||||
private lateinit var binding: ActivityMainBinding
|
private lateinit var binding: ActivityMainBinding
|
||||||
|
|
||||||
|
@ -258,11 +260,20 @@ class MainActivity : NoSplashAppCompatActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onMenuOpened(featureId: Int, menu: Menu): Boolean {
|
override fun onMenuOpened(featureId: Int, menu: Menu): Boolean {
|
||||||
|
menuOpen = true
|
||||||
|
if (binding.mainDrawerLayout.isDrawerOpen(GravityCompat.START)) {
|
||||||
|
binding.mainDrawerLayout.closeDrawers()
|
||||||
|
}
|
||||||
val result = super.onMenuOpened(featureId, menu)
|
val result = super.onMenuOpened(featureId, menu)
|
||||||
menu.findItem(R.id.nav_treatments)?.isEnabled = profileFunction.getProfile() != null
|
menu.findItem(R.id.nav_treatments)?.isEnabled = profileFunction.getProfile() != null
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onPanelClosed(featureId: Int, menu: Menu) {
|
||||||
|
menuOpen = false;
|
||||||
|
super.onPanelClosed(featureId, menu)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
this.menu = menu
|
this.menu = menu
|
||||||
menuInflater.inflate(R.menu.menu_main, menu)
|
menuInflater.inflate(R.menu.menu_main, menu)
|
||||||
|
@ -359,6 +370,22 @@ class MainActivity : NoSplashAppCompatActivity() {
|
||||||
return actionBarDrawerToggle.onOptionsItemSelected(item)
|
return actionBarDrawerToggle.onOptionsItemSelected(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onBackPressed() {
|
||||||
|
if (binding.mainDrawerLayout.isDrawerOpen(GravityCompat.START)) {
|
||||||
|
binding.mainDrawerLayout.closeDrawers()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (menuOpen) {
|
||||||
|
this.menu?.close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (binding.mainPager.currentItem != 0) {
|
||||||
|
binding.mainPager.currentItem = 0
|
||||||
|
return
|
||||||
|
}
|
||||||
|
super.onBackPressed()
|
||||||
|
}
|
||||||
|
|
||||||
// Correct place for calling setUserStats() would be probably MainApp
|
// Correct place for calling setUserStats() would be probably MainApp
|
||||||
// but we need to have it called at least once a day. Thus this location
|
// but we need to have it called at least once a day. Thus this location
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue