自学内容网 自学内容网

Android LoaderManager AsyncTaskLoader,Kotlin(4)

Android LoaderManager AsyncTaskLoader,Kotlin(4)

 

 

import android.content.Context
import androidx.loader.app.LoaderManager
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.loader.content.AsyncTaskLoader
import androidx.loader.content.Loader


class MainActivity : AppCompatActivity() {
    companion object {
        const val TAG = "fly"
    }

    private val loaderId = 2024
    private var manager: LoaderManager? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val ctx = this
        manager = LoaderManager.getInstance(this)
        manager?.initLoader(loaderId, Bundle(), object : LoaderManager.LoaderCallbacks<MyData> {
            override fun onCreateLoader(id: Int, args: Bundle?): Loader<MyData> {
                val loader = MyLoader(ctx)
                Log.d(TAG, "onCreateLoader ${loader.id}")
                return loader
            }

            override fun onLoaderReset(loader: Loader<MyData>) {
                Log.d(TAG, "onLoaderReset ${loader.id}")
            }

            override fun onLoadFinished(loader: Loader<MyData>, data: MyData?) {
                Log.d(TAG, "onLoadFinished ${loader.id} $data")
            }
        })
    }

    override fun onPause() {
        super.onPause()
        //触发 loadInBackground()
        manager?.getLoader<MyData>(loaderId)?.onContentChanged()
    }

    class MyLoader(ctx: Context) : AsyncTaskLoader<MyData>(ctx) {
        override fun onContentChanged() {
            super.onContentChanged()
            Log.d(TAG, "onContentChanged")
        }

        override fun onStartLoading() {
            super.onStartLoading()
            Log.d(TAG, "onStartLoading")
        }

        override fun forceLoad() {
            super.forceLoad()
            Log.d(TAG, "forceLoad")
        }

        override fun loadInBackground(): MyData {
            var d = MyData("${System.currentTimeMillis()}")
            Log.d(TAG, "loadInBackground @ ${d.value}")
            return d
        }

        //工作在主线程。data是loadInBackground()返回的值。
        override fun deliverResult(data: MyData?) {
            super.deliverResult(data)
            Log.d(TAG, "deliverResult ${data?.value}")
        }
    }

    data class MyData(val value: String)
}

 

 

启动后:

D  onCreateLoader 0

D  onStartLoading

 

按home键,把应用切入后台不可见:

D  forceLoad

D onContentChanged

D loadInBackground @ 1729932886262

D  onLoadFinished 2024 MyData(value=1729932886262)

D  deliverResult 1729932886262

 

 

 

 

Android LoaderManager AsyncTaskLoader Kotlin(3)-CSDN博客文章浏览阅读774次,点赞5次,收藏24次。android LoaderManager与AsyncTaskLoader,java(1)_zhangphil的博客-CSDN博客。android LoaderManager与AsyncTaskLoader,java(1)_android loadermanager 和 asynctaskloader。android LoaderManager与AsyncTaskLoader,java(1)_android loadermanager 和 asynctaskloader-CSDN博客。https://zhangphil.blog.csdn.net/article/details/143291905

 


原文地址:https://blog.csdn.net/zhangphil/article/details/143350336

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!