update() 没有在 Activity 内部被调用,这是 Observer

update() not being called inside Activity which is Observer

我的 MainActivity 实现了 Observer class。我还有一个名为 ObservedObject 的 class 扩展了 Observable class.

这是我的习惯 Observable ,叫做 ObservedObject:

class ObservedObject(var value: Boolean) : Observable() {
    init {
        value = false
    }

    fun setVal(vals: Boolean) {
        value = vals
        setChanged()
        notifyObservers()
    }

    fun printVal() {
        Log.i("Value" , "" + value)
    }
}

这是我的 Application,名为 SpeechApp,其中包含我的 ObservedObject(实际上是 Observable):

class SpeechApp: Application() {
    var isDictionaryRead = ObservedObject(false)

    override fun onCreate() {
        super.onCreate()
        wordslist = ArrayList()

        Thread {
            execute()
        }.start()
    }

    fun execute() {
        while (/* Condition */) {
            //Log.i("Read" , line)
            /*Does Something Here*/
        }
        isDictionaryRead.setVal(true)
    }
}

在我的MainActivity中,我主要有一个对话框,它应该在我得到语音识别后的输出后显示。只要 isDictionaryRead 的值不变为 true:

就会显示
class MainActivity(private val REQ_CODE_SPEECH_INPUT: Int = 100) : AppCompatActivity() , Observer{
    override fun update(o: Observable?, arg: Any?) {
        (o as ObservedObject).printVal()
        dialog.hide()
    }

    private lateinit var app : SpeechApp
    private lateinit var dialog: MaterialDialog

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        dialog = MaterialDialog.Builder(this)
                .title("Please Wait")
                .content("Loading from the Dictionary")
                .progress(true , 0)
                .build()

        app = application as SpeechApp
        app.isDictionaryRead.addObserver(this)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_speech, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        val id = item?.itemId
        when(id) {
            R.id.menu_option_speech -> {
                invokeSpeech()
            }
        }
        return super.onOptionsItemSelected(item)
    }

    private fun invokeSpeech() {
        /* Does Something, Works Fine */

        try {
            startActivityForResult(intent , REQ_CODE_SPEECH_INPUT)
        }
        catch (ex: ActivityNotFoundException) {
            /* Does Something */
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        when (requestCode) {
            REQ_CODE_SPEECH_INPUT -> {
                if (resultCode == Activity.RESULT_OK && null != data) {
                    dialog.show()
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data)
    }
}

现在的问题是,当SpeechApp设置isDictionaryRead的值为true时,我希望它调用MainActivityupdate()方法,其中我有给出隐藏对话框的代码。该特定代码不起作用,我的对话框不会消失。我哪里错了?

PS。我现在已经将我的代码推送到 Github,以防万一有人可以帮助我解决我出错的地方。

我唯一能想到的会导致这个问题的是SpeechApp.onCreate中启动的execute()线程结束执行并在之前调用了isDictionaryRead.setVal(true) activity 可以调用 app.isDictionaryRead.addObserver(this)。结果,notifyObservers 在 activity 甚至开始观察之前就被调用了,结果它没有被通知。这是我提出的解决方案:在将 activity 的 onCreate 方法添加为观察者后,启动 execute 线程。

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    dialog = MaterialDialog.Builder(this)
            .title("Please Wait")
            .content("Loading from the Dictionary")
            .progress(true , 0)
            .build()

    app = application as SpeechApp
    app.isDictionaryRead.addObserver(this)
    app.asyncReadDictionary()
}

然后从 SpeechApp.onCreate 中删除线程调用并改用它

// in SpeechApp
fun asyncReadDictionary() {
    if (!isDictionaryRead.value) {
       Thread { execute() }.start()
    }
}

private fun execute() {
    while (/* Condition */) {
        //Log.i("Read" , line)
        /*Does Something Here*/
    }
    isDictionaryRead.value = true
}

另外,重新实现 ObservableObject 如下

class ObservedObject : Observable() {
    var value: Boolean = false
        set(newValue) {
            field = newValue
            setChanged()
            notifyObservers()
        }

    fun printVal() {
        Log.i("Value" , "" + value)
    }
}