Java 如何在刷新活动时保持活动对话框的打开状态(更改应用程序的主题)?
我有一个Java 如何在刷新活动时保持活动对话框的打开状态(更改应用程序的主题)?,java,android,kotlin,android-activity,android-dialog,Java,Android,Kotlin,Android Activity,Android Dialog,我有一个Settings.kt活动,它为我的应用程序提供了主题更改功能。 点击主题选项,屏幕上会弹出一个主题对话框,通过切换,可以改变主题,如下图所示。 现在,为了在设置.kt活动以及主题对话中反映这些更改,我在意图的帮助下刷新设置.kt活动,因此主题正确更改,但是主题对话在活动刷新时同时关闭。 因此,我想知道一种方法,在Settings.kt活动和themeDialog框的主题更新时保持对话框打开 这是我的代码: Settings.kt活动 class Settings : AppCompa
Settings.kt
活动,它为我的应用程序提供了主题更改功能。点击主题选项,屏幕上会弹出一个主题对话框,通过切换,可以改变主题,如下图所示。
现在,为了在
设置.kt
活动以及主题对话中反映这些更改,我在意图的帮助下刷新设置.kt
活动,因此主题正确更改,但是主题对话在活动刷新时同时关闭。
因此,我想知道一种方法,在Settings.kt
活动和themeDialog
框的主题更新时保持对话框打开
这是我的代码:
Settings.kt
活动
class Settings : AppCompatActivity() {
private lateinit var settingsRecyclerView: RecyclerView
private lateinit var settingsAdapter: SettingsAdapter
private lateinit var sharedPrefForNightMode: SharedPrefForNightMode
override fun onCreate(savedInstanceState: Bundle?) {
sharedPrefForNightMode = SharedPrefForNightMode(this)
if (sharedPrefForNightMode.loadNightModeState()) {
setTheme(R.style.DarkTheme)
} else setTheme(R.style.AppTheme)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
settingsAdapter = SettingsAdapter(this)
settingsRecyclerView = findViewById(R.id.settings_recycler_view)
settingsRecyclerView.layoutManager = LinearLayoutManager(this)
settingsRecyclerView.setHasFixedSize(true)
settingsRecyclerView.adapter = settingsAdapter
}
override fun finish() {
super.finish()
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right)
}
override fun onBackPressed() {
super.onBackPressed()
val intent = Intent(this,Dashboard::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
finish()
}
}
SettingsAdapter.kt
-在这个适配器中,我有一个函数theme(context:context)
,它创建我的themeDialog
和restartSettings()
函数刷新Settings.kt
活动:
class SettingsAdapter(private val mContext: Context) :
RecyclerView.Adapter<SettingsAdapter.SettingsViewHolder>() {
private fun theme(context: Context) {
val themeDialog = Dialog(context)
themeDialog.setContentView(R.layout.theme_popup)
themeDialog.setCancelable(false)
themeDialog.show()
val themeTitle = themeDialog.findViewById<TextView>(R.id.theme_title)
val closeTheme = themeDialog.findViewById<Button>(R.id.close_theme)
closeTheme.setOnClickListener {
themeDialog.dismiss()
}
val switch = themeDialog.findViewById<Switch>(R.id.dark_mode_switch)
if(sharedPrefForNightMode.loadNightModeState()) {
switch.isChecked = true
themeTitle.text = "Disable Night Mode"
}
switch.setOnCheckedChangeListener { _, _ ->
if (switch.isChecked) {
sharedPrefForNightMode.setNightModeState(true)
themeTitle.text = "Disable Night Mode"
restartSettings()
} else {
sharedPrefForNightMode.setNightModeState(false)
themeTitle.text = "Enable Night Mode"
restartSettings()
}
}
}
private fun restartSettings() {
mContext.startActivity(Intent(mContext, Settings::class.java))
(mContext as Activity).overridePendingTransition(R.anim.fadein, R.anim.fadeout)
mContext.finish()
mContext.overridePendingTransition(R.anim.fadein, R.anim.fadeout)
}
}
class SettingsAdapter(private val mContext:Context):
RecyclerView.Adapter(){
私人娱乐主题(上下文:上下文){
val主题对话框=对话框(上下文)
主题对话框.setContentView(R.layout.theme\u弹出窗口)
主题对话。可设置取消(false)
主题对话
val themeTitle=themeDialog.findViewById(R.id.theme\u title)
val closeTheme=themeDialog.findviewbyd(R.id.close\u主题)
closeTheme.setOnClickListener{
主题对话
}
val开关=主题对话框.findViewById(R.id.dark\u模式\u开关)
if(SharedPrefornightMode.LoadNightModerate()){
switch.isChecked=true
themeTitle.text=“禁用夜间模式”
}
switch.setOnCheckedChangeListener{{uu,u->
如果(开关已检查){
SharedPrefornightMode.SetNightModerate(真)
themeTitle.text=“禁用夜间模式”
restartSettings()
}否则{
SharedPrefornightMode.SetNightModerate(false)
themeTitle.text=“启用夜间模式”
restartSettings()
}
}
}
私人娱乐餐厅设置(){
startActivity(Intent(mContext,Settings::class.java))
(mContext作为活动)。重写转换(R.anim.fadein,R.anim.fadeout)
mContext.finish()
mContext.overridePendingTransition(R.anim.fadein,R.anim.fadeout)
}
}
如果您需要SettingsAdpater
的完整代码,请告诉我我刚刚发布了与此问题相关的函数对话框
由活动
实例拥有。您正在销毁并重新创建活动,这样将销毁对话框
。您需要在新的活动
实例中自己重新创建对话框
。是的,但这不会在活动刷新时保留对话框。您需要执行一些不涉及销毁和重新创建活动的操作,以影响所需的更改。您应该共享设置主题功能