Android 从另一个活动启动活动会跳过很多(96)帧
我有一个应用程序,它有两个活动-LoginActivity(LA)和AppActivity(AA)。正如您所猜测的,LA在成功登录后启动AA。出于调试目的,我删除了所有登录逻辑,并用启动AA的简单按钮替换它 AA包含一个片段,其中包含一个具有三个视图的viewpager,其中一个视图使用Kotlin co例程读取音乐文件,并填充到Recyclerview中。另外两个视图做了一些不同的事情,不涉及任何类型的文件I/O或api请求,简单的静态UI 每当我启动应用程序时,我都会看到三次跳帧警告Android 从另一个活动启动活动会跳过很多(96)帧,android,kotlin,android-activity,fragment,Android,Kotlin,Android Activity,Fragment,我有一个应用程序,它有两个活动-LoginActivity(LA)和AppActivity(AA)。正如您所猜测的,LA在成功登录后启动AA。出于调试目的,我删除了所有登录逻辑,并用启动AA的简单按钮替换它 AA包含一个片段,其中包含一个具有三个视图的viewpager,其中一个视图使用Kotlin co例程读取音乐文件,并填充到Recyclerview中。另外两个视图做了一些不同的事情,不涉及任何类型的文件I/O或api请求,简单的静态UI 每当我启动应用程序时,我都会看到三次跳帧警告 点击L
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
/*Permissions*/
val permissions = listOf(
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.BLUETOOTH,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO
)
validatePermission(permissions)
}
fun startActivity(view: View) {
val intent = Intent(this, AppActivity::class.java)
startActivity(intent)
finish()
}
private fun validatePermission(permissions: List<String>) {
Dexter.withContext(this)
.withPermissions(permissions)
.withListener(
object : MultiplePermissionsListener {
override fun onPermissionsChecked(p0: MultiplePermissionsReport?) {
}
override fun onPermissionRationaleShouldBeShown(
p0: MutableList<PermissionRequest>?,
p1: PermissionToken?
) {
AlertDialog.Builder(this@LoginActivity)
.setTitle(R.string.storage_permission_rationale_title)
.setMessage(R.string.storage_permission_rationale_message)
.setNegativeButton(
android.R.string.cancel,
DialogInterface.OnClickListener { dialogInterface, i ->
dialogInterface.dismiss()
p1?.cancelPermissionRequest()
})
.setPositiveButton(
android.R.string.ok,
DialogInterface.OnClickListener { dialogInterface, _ ->
dialogInterface.dismiss()
p1?.continuePermissionRequest()
})
.show()
}
}
).check()
}
我还注释掉了viewPager和其他两个跳过帧警告。现在,我不想删除viewpager,所以我仍在调试,很快就会在这里更新。在一些应用程序中,我遇到了相同的问题
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_app)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(setOf(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow), drawerLayout)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
// Oncreate is the place for the initialization of things but if their initialization is CPU intensive then delaying them a bit will make things hassle-free.
Handler(Looper.getMainLooper()).postDelayed({
engine = TextToSpeech(this, this)
observers()//perform other intensive task after a delay,e.g. loading a url on webview.
},200)//delayed by 200ms,
}
当我们开始一项活动时,一旦oncreate完成并调用onstart,活动就会立即启动,因此我们必须让
oncreate
顺利完成,这将使活动立即启动。您可以添加AppActivity
的代码吗我找到了三个跳帧警告之一。我在onCreate inside AppCitivity中初始化TTS engine-engine=TextToSpeech(这个,这个),这导致跳过84帧,所以我注释掉了,现在我只收到两个跳过帧的警告,而不是三个。那么,现在你不需要了?不,我需要它,所以我把它放在一个线程中。现在我正试图找出另外两个跳过帧的警告。如果你能添加代码,你会得到帮助。我猜你的延迟初始化方法是有效的。非常感谢。我以为我写了一些愚蠢的代码。再次感谢你。我可以在聊天中再问几个问题吗?我在聊天中添加了一个问题。请随时检查。谢谢当然,我会查出来的。@Greyfrog,请查出来聊天,我提到了一些建议。再次非常感谢。我正在阅读你提供的链接。还有一件事我已经弄明白了,那就是,在我与当前视图中的任何元素交互之前,CPU使用率保持为零。一旦交互,CPU使用率就永远不会回到零,即使您不再与UI交互。因此,罪魁祸首不是mediaplayer。我试着用简单的seekbar。当我滑动到视图时,CPU使用率保持为零,但只要我调整seekbar,CPU使用率就会上升,即使我不再与它交互,也会保持上升。你能猜出原因吗?
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_app)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(setOf(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow), drawerLayout)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
// Oncreate is the place for the initialization of things but if their initialization is CPU intensive then delaying them a bit will make things hassle-free.
Handler(Looper.getMainLooper()).postDelayed({
engine = TextToSpeech(this, this)
observers()//perform other intensive task after a delay,e.g. loading a url on webview.
},200)//delayed by 200ms,
}