调用 startActivity 和启动 onCreate 之间存在巨大的时间滞后

问题描述 投票:0回答:1

我遇到了调用 startActivity() 和正在启动的活动的 onCreate() 方法的第一条指令之间的时间滞后问题。我实际上添加了代码来测量该时间,平均时间超过 20 秒。

这是原始活动中的重要代码,称为 CameraActivity:

companion object{
    lateinit var time1 : ZonedDateTime
    lateinit var time2 : ZonedDateTime
    lateinit var time3 : ZonedDateTime
    ...
}
...
private fun takePicture(){

    waitDialog = MyDialogFragment(getString(R.string.wait),false,this)
    waitDialog.show(supportFragmentManager,"") //why isn't the dialog showing???
    try {
        time1 = ZonedDateTime.now()
        val cellGrid = ImageProcessing.getCellGrid(currentImage)
        val puzzle = Puzzle(MLUtilities.predictPuzzle(cellGrid))
        val intent = Intent(thisActivity, GameActivity::class.java)
        intent.putExtra("case", Case.CAMERA.toString())
        intent.putExtra("stage", Stage.INPUT.toString())
        repeat(9) { i ->
            repeat(9) { j ->
                intent.putExtra("value$i$j",puzzle.get(i, j))
            }
        }
        time2 = ZonedDateTime.now()
        thisActivity.startActivity(intent)

    }catch(e: Exception){
        Log.e(MainActivity.TAG,"",e)
        waitDialog.dismiss()
    }
    finally {
        waitDialog.dismiss()
    }
}

这是正在调用的活动的代码:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    CameraActivity.time3 = ZonedDateTime.now()
    Log.d(MainActivity.TAG,"This is how long it takes to recognize the puzzle and load it into intent (in seconds): "+((CameraActivity.time2.toInstant().toEpochMilli()-CameraActivity.time1.toInstant().toEpochMilli()).toDouble()/1000).toString())
    Log.d(MainActivity.TAG,"This is how long it is taking between the startActivity method being called and")
    Log.d(MainActivity.TAG,"the onCreate finally launching (in seconds): "+((CameraActivity.time3.toInstant().toEpochMilli()-CameraActivity.time2.toInstant().toEpochMilli()).toDouble()/1000).toString())
...
}

我运行了此代码六次,并用它在日志中记录了重要的持续时间(删除了会使其难以阅读的部分):

This is how long it takes to recognize the puzzle and load it into intent (in seconds): 2.189
This is how long it is taking between the startActivity method being called and
the onCreate finally launching (in seconds): 37.806

This is how long it takes to recognize the puzzle and load it into intent (in seconds): 2.099
This is how long it is taking between the startActivity method being called and
the onCreate finally launching (in seconds): 20.074

This is how long it takes to recognize the puzzle and load it into intent (in seconds): 2.123
This is how long it is taking between the startActivity method being called and
the onCreate finally launching (in seconds): 53.911

This is how long it takes to recognize the puzzle and load it into intent (in seconds): 0.69
This is how long it is taking between the startActivity method being called and
the onCreate finally launching (in seconds): 19.981

This is how long it takes to recognize the puzzle and load it into intent (in seconds): 0.843
This is how long it is taking between the startActivity method being called and
the onCreate finally launching (in seconds): 5.461

This is how long it takes to recognize the puzzle and load it into intent (in seconds): 1.014
This is how long it is taking between the startActivity method being called and
the onCreate finally launching (in seconds): 19.813

另一个似乎与之相关的问题(尽管它可能是单独的)是 waitDialog 没有显示。它的目的是作为一条消息,明确在用户等待时正在处理捕获的图像。

我真的很感谢任何帮助让这个工作正常进行,因为它让我最努力的功能看起来像是滞后的,而实际上并非如此。它破坏了一款原本功能完美的游戏,我不知道为什么会发生这种情况。

值得一提的是,尽管我认为这不是问题的根源,CameraActivity 是 Views Camera2 API Activity,而 GameActivity 是基于 Compose 的 Activity。

android kotlin android-jetpack-compose android-camera2
1个回答
0
投票

这可能是因为您出于意图将 81 个额外值放入 Bundle 中。

repeat(9) { i ->
            repeat(9) { j ->
                intent.putExtra("value$i$j",puzzle.get(i, j))
            }
        }

您可以只放入一个物体。 你可以通过多种方式做到这一点,我只是选择这种方式,因为我不知道你的拼图域数据:

val values = mutableHashMap<String, Int>()
repeat(9) { i ->
            repeat(9) { j ->
                values["value$i$j"] = puzzle.get(i, j)
            }
          }
intent.putExtra("puzzleValues", MyPuzzleValues(values))



@Parcelize
data class MyPuzzleValues(values : HashMap<String, Int>)

然后你把它从另一边拿出来,就像:

data = intent?.getParcelableExtra<MyPuzzleValues>("puzzleValues")
© www.soinside.com 2019 - 2024. All rights reserved.