在Observer的Activity中未调用update()

时间:2018-07-01 14:11:40

标签: android kotlin observer-pattern

我的MainActivity实现了Observer类。我还有一个名为ObservedObject的类,它扩展了Observable类。

这是我的自定义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)
    }
}

现在的问题是,当SpeechAppisDictionaryRead的值设置为true时,我希望它调用MainActivity update()方法,其中我给出了隐藏对话框的代码。该特定代码不起作用,并且我的对话框也没有消失。我要去哪里错了?

PS。现在,我已将代码推送到Github,以防万一有人可以帮助我解决我的问题。

1 个答案:

答案 0 :(得分:1)

我唯一想到的是会导致此问题的原因是,在execute()中启动的SpeechApp.onCreate线程已完成执行并被称为isDictionaryRead.setVal(true) 之前活动可以调用app.isDictionaryRead.addObserver(this)。结果,notifyObservers在活动开始观察之前就被调用,结果没有通知。这是我建议的解决方案:将活动添加为观察者后,在活动的execute方法中启动onCreate线程。

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)
    }
}