kotlin 获取回收器适配器中的活动onDestroy()

dwbf0jvd  于 2022-12-13  发布在  Kotlin
关注(0)|答案(4)|浏览(197)

我正在RecyclerView列表项中使用一个处理程序来显示一个计时器。当我按回承载RecyclerViewActivity时,Handler()将被完全破坏,Handler()仍在后台运行。处理程序在ViewHolder中创建并初始化。有没有办法从ViewHolder本身中删除处理程序的回调?
My ViewHolder示例代码

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), CustomRunnable.CustomRunnableListener{

    private val handler = Handler()
    lateinit var customRunnable: CustomRunnable //custom runnable for my timer logic

    fun bind(position: Int, listModelClass: ModelClass?){

        if(someCondition){
                customRunnable = CustomRunnable(handler, this, textView, listModelClass)
                handler.postDelayed(customRunnable, 1000)
        }

    }

    override fun onTimerFinish(listModelClass: ModelClass) {
            // I get this call back when the timer finishes
            handler.removeCallbacks(customRunnable)
    }

}
llew8vvj

llew8vvj1#

据我所知,当RecyclerView从活动中分离时,适配器上没有调用的方法。
尝试在BaseActivityApplication类中创建一个计时器对象或对象列表,并在按下onBack后运行一个停止该计时器的方法。

//Declare timer
    CountDownTimer cTimer = null;

    //start timer function
    void startTimer() {
        cTimer = new CountDownTimer(30000, 1000) {
            public void onTick(long millisUntilFinished) {
            }
            public void onFinish() {
            }
        };
        cTimer.start();
    }

    //cancel timer
    void cancelTimer() {
        if(cTimer!=null)
            cTimer.cancel();
    }
pwuypxnk

pwuypxnk2#

您可以在活动的**onDestory()**中执行此操作

handler.removeCallbacksAndMessages(null);

删除obj为token的回调和已发送消息的任何挂起发布。如果token为null,则将删除所有回调和消息。

rhfm7lfc

rhfm7lfc3#

如果您的Activity是从androidx.activity.ComponentActivity扩展而来的,那么您可以轻松地做到这一点,只需绑定生命周期事件,然后超类就会按照您的要求完成它们的工作,示例代码如下所示:

internal class SampleViewHolder(
    private val activity: TheActivity,
    view: View
) : RecyclerView.ViewHolder(view) {

    fun bind(item: SampleInfo, position: Int) {
        val view = itemView
        ...
        bindLifecycle()
    }

    fun onViewRecycled() {
        itemView.imv_sample.onViewRecycled(sampleAdapter.playStateStore)
        activity.lifecycle.removeObserver(lifecycleObserver)
    }

    private val lifecycleObserver = object : LifecycleObserver {
        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        private fun onDestroy() {
            MLog.info(TAG, "receive destroy event")
            itemView.imv_sample.onHostActivityDestroyed()
        }
    }

    private fun bindLifecycle() {
        activity.lifecycle.run {
            MLog.info(TAG, "success bind lifecycle")
            addObserver(lifecycleObserver)
        }
    }
}
rmbxnbpk

rmbxnbpk4#

销毁Activity时,将列表适配器设置为null。这将确保在销毁Activity时为列表中的所有视图调用onViewDetachedFromWindow

@Override
public void onDestroy() {
    mList.setAdapter(null);
    super.onDestroy();
}

然后,您可以从viewHolder内部运行的处理程序中删除回调。这需要您将处理程序引用保存在viewHolder内部。

@Override
public void onViewDetachedFromWindow(@NonNull PurchaseItemViewHolder holder) {
    if (holder.handler != null) {
        holder.handler.removeCallbacksAndMessages(null);
    }
    super.onViewDetachedFromWindow(holder);
}

相关问题