In Splashscreen Activity inside I implemented the interface. How to send data of interface to multiple Activity in Kotlin?

孤者浪人 提交于 2020-01-11 10:50:20

问题


Interface class:

interface MyCustomInterface {
     fun get_msg( str: String)

}

SplashScreen Activity:

class SplashActivity : AppCompatActivity(),MyCustomInterface {
    override fun get_msg(str: String) {

    }

val socket=MysocketIO(this)
    }

MySocketIO class where I added the data to interface from server:

class MysocketIO(var mainActivity: SplashActivity) : Thread() {
    fun enable_data_event():Boolean {
        try {
            data_event_registered.set(true)
            socket!!.on(this.data_event_id, object : Emitter.Listener {
                override fun call(vararg args: Any) {
                    receive_data_queue.add(args[0] as String)
                    data_recieved.set(true)
                    mainActivity.get_msg(receive_data_queue.peek())
                }
            })
            return true
        }
        catch (ex : java.lang.Exception){
            ex.printStackTrace()
        }
    }
}

How to get data from SplashScreen to another activity?


回答1:


Try to create one global veriable to store interface data after then pass intent to activity.. like ..

class SplashActivity : AppCompatActivity(),MyCustomInterface{
var data=""
override fun get_msg(str: String) {
    data=str
}

after when to start any activity to pass data into intent like..

intent.putString(key,data)


来源:https://stackoverflow.com/questions/55644972/in-splashscreen-activity-inside-i-implemented-the-interface-how-to-send-data-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!