使用startActivityIntent转到下一个活动时,应用程序不断崩溃

问题描述

我的应用程序中存在一个错误,即同时更新Firebase实时数据库中的两个根级节点导致该应用程序发生小型崩溃,并且startActivity(Intent)失败,并且返回到基本活动。

fun makeNewChannel(currentUser: String,channelname: String) {
    val memberkey = fb.child("/channel/$channelname/members").push().key.toString()

    val updateChannel = HashMap<String,Any?>()
    updateChannel.put("/channel/$channelname/members/$memberkey",currentUser)
    updateChannel.put("/channel/$channelname/name",channelname)

    val updateUser = HashMap<String,Any?>()
    updateUser.put("/user/$currentUser/channels/$channelname",memberkey)

    fb.updateChildren(updateUser)
    fb.updateChildren(updateChannel)}

如果我注释掉了updateUser(位于另一个节点上的updateUser),则startActivity不会崩溃,并且应用程序会平稳过渡到下一个活动。我假设这可能是由于/ channel节点上的侦听器,但是我真的不知道。

解决方法

更改此...

val myIntent = Intent(this@ChannelListActivity,ChannelActivity::class.java)
        myIntent.putExtra("chan_name",channelList[index])
        Log.d(
        "toChannel2","Clicked channel from channel list,moving to ChannelActivity"
        )
        startActivity(myIntent)
        finish()

对此...

val myIntent = Intent(this@ChannelListActivity,ChannelActivity::class.java)

        // // The myIntent.flags tells to clear/close the @CurrentFragment/@CurrentActivity and inflate/start @NextFragment/@NextActiviy
        myIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK.or(Intent.FLAG_ACTIVITY_NEW_TASK)
        Log.d(
        "toChannel2",moving to ChannelActivity"
        )
        startActivity(myIntent)
        finish()