diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.kt b/app/src/main/java/info/nightscout/androidaps/MainActivity.kt index 7ef621de3b..48296330cf 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.kt @@ -21,6 +21,7 @@ import android.widget.TextView import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.AlertDialog import androidx.appcompat.widget.Toolbar +import androidx.core.view.GravityCompat import androidx.viewpager2.widget.ViewPager2 import com.google.android.material.tabs.TabLayoutMediator import com.google.firebase.crashlytics.FirebaseCrashlytics @@ -87,6 +88,7 @@ class MainActivity : NoSplashAppCompatActivity() { private lateinit var actionBarDrawerToggle: ActionBarDrawerToggle private var pluginPreferencesMenuItem: MenuItem? = null private var menu: Menu? = null + private var menuOpen = false private lateinit var binding: ActivityMainBinding @@ -258,11 +260,20 @@ class MainActivity : NoSplashAppCompatActivity() { } 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) menu.findItem(R.id.nav_treatments)?.isEnabled = profileFunction.getProfile() != null return result } + override fun onPanelClosed(featureId: Int, menu: Menu) { + menuOpen = false; + super.onPanelClosed(featureId, menu) + } + override fun onCreateOptionsMenu(menu: Menu): Boolean { this.menu = menu menuInflater.inflate(R.menu.menu_main, menu) @@ -359,6 +370,22 @@ class MainActivity : NoSplashAppCompatActivity() { 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 // but we need to have it called at least once a day. Thus this location