Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android : pause / stop webview in recyclerview

I am trying to display youtube videos in RecyclerView. Everything works absolutely fine, but I came across issue (I am new to it). After reading some posts(Post 1, Post 2), I am, now, aware about the issue that WebView thread never stops even if application stops or goes in background.

My issue is handling the pause of WebView in RecyclerView.Adapter. I have created RecyclerView adapter, but I am having not idea, how to stop a WebView in ViewHolder in RecyclerView.Adapter.

If anyone could help me please.

like image 752
Chintan Soni Avatar asked Oct 20 '25 04:10

Chintan Soni


2 Answers

Many thanks to @cwbowron. Just to make it more clear, I have pasted my code here. We need to override two methods to make it work as expected:

onViewAttachedToWindow() is called when a view created by this adapter has been attached to a window.

onViewDetachedFromWindow() is called when a view created by this adapter has been detached from its window.

And this is what I needed. Below is my implementation:

public class RecyclerAdapterMessage extends RecyclerView.Adapter<RecyclerAdapterMessage.MessagesViewHolder> {

    private Context mContext;
    private ArrayList<ModelInboxFeed> modelInboxFeedArrayList;
    private OnUserProfileClickListener mOnUserProfileClickListener;

    public RecyclerAdapterMessage(Activity context, ArrayList<ModelInboxFeed> modelInboxFeedArrayList) {
        ...
    }

    /**
     * Create new views (invoked by the layout manager)
     */
    @Override
    public RecyclerAdapterMessage.MessagesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_card_normal, parent, false);
        return new MessagesViewHolder(mContext, view);
    }

    /**
     * Replace the contents of a view (invoked by the layout manager)
     */
    @Override
    public void onBindViewHolder(final RecyclerAdapterMessage.MessagesViewHolder holder, int position) {
        ...
    }

    /**
     * Return the size of your data set (invoked by the layout manager)
     */
    @Override
    public int getItemCount() {
        return modelInboxFeedArrayList.size();
    }

    public interface OnUserProfileClickListener {
        void onUserProfileClick();
    }

    public class MessagesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
       private WebView mWebView;
       ...
    }

    @Override
    public void onViewDetachedFromWindow(MessagesViewHolder holder) {
        super.onViewDetachedFromWindow(holder);

        holder.mWebView.onPause();
    }

    @Override
    public void onViewAttachedToWindow(MessagesViewHolder holder) {
        super.onViewAttachedToWindow(holder);

        holder.mWebView.onResume();
    }
}

Hope it helps anyone with same requirement.

Happy Coding..!!

like image 165
Chintan Soni Avatar answered Oct 24 '25 19:10

Chintan Soni


In RecyclerView.Adapter there are life-cycle callbacks for ViewHolders. You should be able to override onViewDetachedFromWindow to stop the WebView.

like image 33
cwbowron Avatar answered Oct 24 '25 18:10

cwbowron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!