如何在配置更改时恢复菜单状态
How to restore the menu state on configuration change
我有 2 个菜单项(过滤按钮和搜索按钮)允许在列表中过滤或搜索。 When a list item is selected, it is given to the view throught the viewModel's LiveData called listItemSelected
, when this happen, we move from the ListFragment
to the DetailFragment
and hide the 2 buttons from菜单,因为它们与此处无关。
我的问题是配置更改,例如屏幕旋转,旧的 activity 被破坏,新的 Activity.onCreate()
函数在 onCreateOptionsMenu()
之前被调用,所以我没有不知道如何正确设置菜单状态。
下面的简化代码示例,现在它只是在 filterMenuItem.isVisible = it == null
上崩溃,因为此时 filterMenuItem
尚未初始化。
class MyActivity : AppCompatActivity() {
private lateinit var viewModel: MyViewModel
private lateinit var filterMenuItem: MenuItem
private lateinit var searchMenuItem: MenuItem
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = getViewModel { injector.myViewModel }
viewModel.listItemSelected.observe(this, Observer {
filterMenuItem.isVisible = it == null
searchMenuItem.isVisible = it == null
})
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.main_menu, menu)
menu!!.apply {
filterMenuItem = findItem(R.id.main_menu_filter)
searchMenuItem = findItem(R.id.main_menu_search)
}
return true
}
初始化 MenuItem
后如何观察数据:
class MyActivity : AppCompatActivity() {
private lateinit var viewModel: MyViewModel
private lateinit var filterMenuItem: MenuItem
private lateinit var searchMenuItem: MenuItem
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = getViewModel { injector.myViewModel }
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.main_menu, menu)
menu!!.apply {
filterMenuItem = findItem(R.id.main_menu_filter)
searchMenuItem = findItem(R.id.main_menu_search)
}
// observe after the menu items are initialized
viewModel.listItemSelected.observe(this, Observer {
filterMenuItem.isVisible = it == null
searchMenuItem.isVisible = it == null
})
return true
}
}
我有 2 个菜单项(过滤按钮和搜索按钮)允许在列表中过滤或搜索。 When a list item is selected, it is given to the view throught the viewModel's LiveData called listItemSelected
, when this happen, we move from the ListFragment
to the DetailFragment
and hide the 2 buttons from菜单,因为它们与此处无关。
我的问题是配置更改,例如屏幕旋转,旧的 activity 被破坏,新的 Activity.onCreate()
函数在 onCreateOptionsMenu()
之前被调用,所以我没有不知道如何正确设置菜单状态。
下面的简化代码示例,现在它只是在 filterMenuItem.isVisible = it == null
上崩溃,因为此时 filterMenuItem
尚未初始化。
class MyActivity : AppCompatActivity() {
private lateinit var viewModel: MyViewModel
private lateinit var filterMenuItem: MenuItem
private lateinit var searchMenuItem: MenuItem
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = getViewModel { injector.myViewModel }
viewModel.listItemSelected.observe(this, Observer {
filterMenuItem.isVisible = it == null
searchMenuItem.isVisible = it == null
})
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.main_menu, menu)
menu!!.apply {
filterMenuItem = findItem(R.id.main_menu_filter)
searchMenuItem = findItem(R.id.main_menu_search)
}
return true
}
初始化 MenuItem
后如何观察数据:
class MyActivity : AppCompatActivity() {
private lateinit var viewModel: MyViewModel
private lateinit var filterMenuItem: MenuItem
private lateinit var searchMenuItem: MenuItem
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = getViewModel { injector.myViewModel }
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.main_menu, menu)
menu!!.apply {
filterMenuItem = findItem(R.id.main_menu_filter)
searchMenuItem = findItem(R.id.main_menu_search)
}
// observe after the menu items are initialized
viewModel.listItemSelected.observe(this, Observer {
filterMenuItem.isVisible = it == null
searchMenuItem.isVisible = it == null
})
return true
}
}