Overview
In some business scenarios, in order to improve the user experience, we may need to prepare the data of the next page on the previous page to reduce the time for users to follow-up operations. Because Activity is designed with low coupling and high isolation, the traditional pre-loading Activity solution is not elegant enough. Now we provide a more elegant pre-loading solution that can pre-load the layout and page data.
github:https://github.com/long8313002/PreloadingActivity
Show results
use
Note: Because the library uses Kotlin development, you need to configure the Kotlin development environment in the project
Library reference
implementation 'com.zhangzheng.preloading.activity:library:1.0.0'
Usage example
val intent = Intent(this,TestActivity::class.java)
intent.putExtra("id",1111)
PreLoading.preLoading(this,intent,TestPreLoadingView::class.java)
textView.setOnClickListener {
startActivity(intent)
}
By default, when the preload page is opened and the page is destroyed, the preloaded instance will also be destroyed. You need to reload the layout and data next time. If you need to keep the preloaded data and reuse it multiple times, you can set the parameter autoDestroy = false as follows:
PreLoading.preLoading(this,intent,TestPreLoadingView::class.java,false)
Preload page implementation
Taking into account that the base class Activity used by different projects will be different, for versatility, the Activity is not directly customized, but AbsPreLoadingView is provided to achieve it through combination. For ease of use, a base class inherited from Activity is provided here. In actual use, you can refer to this class to implement your own base class, as follows:
Demonstration base class
abstract class AbsPreLoadingActivity : Activity() {
abstract fun preLoadingViewClass() : KClass<out AbsPreLoadingView>
private lateinit var preLoadingView: AbsPreLoadingView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
preLoadingView =preLoadingViewClass().getOrCreate(intent, this)
setContentView(preLoadingView)
preLoadingView.callCreate(savedInstanceState)
}
override fun onStart() {
super.onStart()
preLoadingView.callStart()
}
override fun onResume() {
super.onResume()
preLoadingView.callResume()
}
override fun onPause() {
super.onPause()
preLoadingView.callPause()
}
override fun onStop() {
super.onStop()
preLoadingView.callStop()
}
override fun onDestroy() {
super.onDestroy()
preLoadingView.callDestroy()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
preLoadingView.onActivityResult(requestCode, resultCode, data)
}
}
Use demonstration
class TestActivity : AbsPreLoadingActivity() {
override fun preLoadingViewClass() = TestPreLoadingView::class
}
class TestPreLoadingView(context: Context) : AbsPreLoadingView(context) {
override fun resId() = R.layout.activity_test_list
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestTestData(intent?.getIntExtra("id",0)?:0)
}
override fun onResume() {
super.onResume()
}
private fun requestTestData(id:Int){
Thread {
Thread.sleep(1000)
val listData = ArrayList<String>()
listData.add("$id === > 1")
listData.add("$id === > 2")
listData.add("$id === > 3")
listData.add("$id === > 4")
listData.add("$id === > 5")
listData.add("$id === > 6")
listData.add("$id === > 7")
listData.add("$id === > 8")
listData.add("$id === > 9")
listData.add("$id === > 10")
listData.add("$id === > 11")
listData.add("$id === > 12")
Handler(Looper.getMainLooper()).post {
listview.adapter = ArrayAdapter(context,android.R.layout.simple_list_item_1,listData)
}
}.start()
}
}