4

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.

Community
  • 1
  • 1
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174

2 Answers2

6

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..!!

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
4

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

cwbowron
  • 1,015
  • 6
  • 10
  • Okay sir.. I am on it.. I will try and update you. thank you.. :) – Chintan Soni Dec 18 '15 at 05:33
  • Omg.. Its working like a charm.. Thank you sir.. You saved my day.. Yesterday, I have been searching and trying to get rid out of this issue for last 4-5 hours... but when I tried this, it was like.. woah.. :) – Chintan Soni Dec 18 '15 at 05:43