在Android中清理资源的最佳方法是什么? [英] What is the best way to clear up resources in Android?

查看:81
本文介绍了在Android中清理资源的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ExoPlayer像Spotify一样构建应用程序.我会在歌曲开始播放时启动播放器通知,即使将应用置于后台也应该开始播放.因此,当用户故意从Ram清除应用程序但onDestory()作为释放播放器并保存最后播放的歌曲.android.com/reference/android/app/Activity.html#onDestroy()"rel =" nofollow noreferrer>在此声明.因此,我想在自定义 Application 中的onActivityDestroyed()中执行资源清理,但这也失败了.

override fun onActivityDestroyed(activity: Activity?) {
                val activityName = activity!!.localClassName
                Log.d(TAG, "onActivityDestroyed: activity name ==> $activityName")

                val musicPlayerDAO =
                    MusicPlayerDatabase.getDatabase(applicationContext).musicPlayerDao()
                val repository = Repository(musicPlayerDAO)

                val job = Job()
                CoroutineScope(Dispatchers.IO + job).launch {
                    repository.insertLastPlayedSong(LastPlayedSongEntity("Dummy title", 3000))                    
                }

                Log.d(TAG, "onActivityDestroyed: Just after the co-routine")
            }

此处仅执行第一个日志.在这种情况下,释放资源的最佳可能方法是什么?

解决方案

您应该释放onStop()中的资源,当活动进入后台时,资源将停止但不会被破坏(除非系统终止了活动的托管进程).释放onDestroy()中的资源,当Activity进入后台时,默认情况下不会调用它,Activity在停止状态下将保留这些资源,从而导致您的应用在后台状态下消耗更多的资源.

I am building an application just like Spotify using ExoPlayer. I am starting a player notification when the song starts playing and it should be getting played even when the app is put into the background. Therefore, I need to release the player and save the last played song when the user deliberately clears the app off from the Ram but onDestory() is not reliable as stated here. So, I thought doing resource cleaning in onActivityDestroyed() in a custom Application but that failed too.

override fun onActivityDestroyed(activity: Activity?) {
                val activityName = activity!!.localClassName
                Log.d(TAG, "onActivityDestroyed: activity name ==> $activityName")

                val musicPlayerDAO =
                    MusicPlayerDatabase.getDatabase(applicationContext).musicPlayerDao()
                val repository = Repository(musicPlayerDAO)

                val job = Job()
                CoroutineScope(Dispatchers.IO + job).launch {
                    repository.insertLastPlayedSong(LastPlayedSongEntity("Dummy title", 3000))                    
                }

                Log.d(TAG, "onActivityDestroyed: Just after the co-routine")
            }

Only the first log is getting executed here. What is the best possible way of freeing up resources in this case?

解决方案

You should free your resource in onStop(), when activity goes into background it is stopped but not destroyed(unless the system kills the activity's hosting process).If you release resources in onDestroy(), which is not called by default when Activity goes to background, the Activity will hold to these resources while in stopped state, thus causing higher amount of resources to be consumed by your app in background state.

这篇关于在Android中清理资源的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆