简体   繁体   English

Kotlin 图标设置在服务中

[英]Kotlin icon setting in service

In the application I wrote with kotlin, I get status information from the server with the service every minute.在我使用 kotlin 编写的应用程序中,我每分钟从服务器获取状态信息。 I want to change the menu icon according to the status information from the server.我想根据服务器的状态信息更改菜单图标。 I created a static global menu variable for this, but the icon changes only once, even if the status information from the server changes, the icon does not change.我为此创建了一个 static 全局菜单变量,但图标只改变一次,即使来自服务器的状态信息发生变化,图标也不会改变。

MainActivitiy.kt MainActivity.kt

companion object {
    lateinit var menum: Menu
}

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
    menuInflater.inflate(R.menu.menu_main, menu)
    menum = menu;
    ....
    ....
}

Service.kt服务.kt

    fun sync_check(){
    if(LocalDbHash==SrvDbHash){
        MainActivity.menum.findItem(R.id.sync_btn).setIcon(R.drawable.ic_edit)
    }else{
        MainActivity.menum.findItem(R.id.sync_btn).setIcon(R.drawable.ic_delete)
    }
}

You want onPrepareOptionsMenu instead :你想要onPrepareOptionsMenu代替

After the system calls onCreateOptionsMenu() , it retains an instance of the Menu you populate and will not call onCreateOptionsMenu() again unless the menu is invalidated for some reason.系统调用onCreateOptionsMenu()后,它会保留您填充的Menu的一个实例,并且不会再次调用onCreateOptionsMenu() ,除非菜单因某种原因而无效。 However, you should use onCreateOptionsMenu() only to create the initial menu state and not to make changes during the activity lifecycle .但是,您应该只使用onCreateOptionsMenu()来创建初始菜单 state 而不要在活动生命周期中进行更改

If you want to modify the options menu based on events that occur during the activity lifecycle, you can do so in the onPrepareOptionsMenu() method.如果要根据活动生命周期中发生的事件修改选项菜单,可以在onPrepareOptionsMenu()方法中进行。 This method passes you the Menu object as it currently exists so you can modify it, such as add, remove, or disable items.此方法向您传递当前存在的Menu object,以便您可以对其进行修改,例如添加、删除或禁用项目。

On Android 3.0 and higher, the options menu is considered to always be open when menu items are presented in the app bar.在 Android 3.0 及更高版本上,当应用栏中显示菜单项时,选项菜单被视为始终打开。 When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu()当一个事件发生,你想执行一个菜单更新,你必须调用invalidateOptionsMenu()来请求系统调用onPrepareOptionsMenu()

So syncCheck should just set a "current icon" variable, and call invalidateOptionsMenu() .所以syncCheck应该只设置一个“当前图标”变量,然后调用invalidateOptionsMenu() onPrepareOptionsMenu will be passed the current Menu and you can do your update there. onPrepareOptionsMenu将传递当前Menu ,您可以在那里进行更新。 Something like:就像是:

var syncButtonEdits = true

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    // inflate the basic menu
    menuInflater.inflate(R.menu.menu_main, menu)
}

// this is called just before the menu is displayed
override fun onPrepareOptionsMenu(menu: Menu): Boolean {
    menu.findItem(R.id.sync_btn).setIcon(
        // set this icon according to the current state
        if (syncButtonEdits) R.drawable.ic_edit else R.drawable.ic_delete
    )
}

fun sync_check(){
    syncButtonEdits = LocalDbHash==SrvDbHash
    // calls onPrepareOptionsMenu, which will check the value of syncButtonEdits
    invalidateOptionsMenu()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM