Skip to content

Commit

Permalink
Save menu state
Browse files Browse the repository at this point in the history
  • Loading branch information
Semper-Viventem committed Jul 21, 2018
1 parent 71ab074 commit b22ea51
Showing 1 changed file with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.semper_viventem.backdropview.ui

import android.os.Bundle
import android.support.annotation.IdRes
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
Expand All @@ -14,6 +15,8 @@ import ru.semper_viventem.backdropview.ui.text.TextScreen
class MainActivity : AppCompatActivity() {

companion object {
private const val ARG_LAST_MENU_ITEM = "last_menu_item"

private const val MENU_GALLERY = R.id.menuGallery
private const val MENU_TEXT = R.id.menuText
private const val MENU_LIST = R.id.menuList
Expand All @@ -23,8 +26,6 @@ class MainActivity : AppCompatActivity() {
private const val DEFAULT_ITEM = MENU_GALLERY
}

private val defaultPage = GalleryScreen()

private lateinit var backdropBehavior: BackdropBehavior

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -41,17 +42,14 @@ class MainActivity : AppCompatActivity() {
}

navigationView.setNavigationItemSelectedListener { item ->
when (item.itemId) {
MENU_GALLERY -> showPage(GalleryScreen())
MENU_TEXT -> showPage(TextScreen())
MENU_LIST -> showPage(ListScreen())
}
checkMenuPosition(item.itemId)
backdropBehavior.close()
true
}

navigationView.setCheckedItem(DEFAULT_ITEM)
showPage(defaultPage)
val currentItem = savedInstanceState?.getInt(ARG_LAST_MENU_ITEM) ?: DEFAULT_ITEM
navigationView.setCheckedItem(currentItem)
checkMenuPosition(navigationView.checkedItem!!.itemId)
}

override fun onBackPressed() {
Expand All @@ -60,6 +58,20 @@ class MainActivity : AppCompatActivity() {
}
}

override fun onSaveInstanceState(outState: Bundle) {
outState.putInt(ARG_LAST_MENU_ITEM, navigationView.checkedItem!!.itemId)

super.onSaveInstanceState(outState)
}

private fun checkMenuPosition(@IdRes menuItemId: Int) {
when (menuItemId) {
MENU_GALLERY -> showPage(GalleryScreen())
MENU_TEXT -> showPage(TextScreen())
MENU_LIST -> showPage(ListScreen())
}
}

private fun showPage(page: Fragment) {
supportFragmentManager
.beginTransaction()
Expand Down

0 comments on commit b22ea51

Please sign in to comment.