Strict mode in recycler view adapter -> inflate method

33 views Asked by At

I've activated strict mode in my APP, and I find a strange thing: when I create a new adapter for my recycler view, the system logs a [StrictMode policy violation; ~duration=352 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=1114143 violation=2].

This happens on my onCreateViewHolder method, that inflates the view, in this way:


@NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType)
    {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cliente_centro_list_item, parent, false));
    }

and my view holder is:

private static class ViewHolder extends RecyclerView.ViewHolder
    {
        private final View viewCompleta;
        private final TextView tvMissioni;
        private final TextView tvUltimoUsato;
        private final TextView tvOrdiniCarico;
        private final TextView tvInfoSecondaria;
        private final TextView tvRagioneSociale;
        private final LinearLayout boxContatori;
        private final TextView tvOrdiniSpedizione;
        private final TextView tvOrdiniProduzione;
        private final TextView tvOrdiniTrasferimento;

        /**
         * Costruttore.
         * @param view gestore view.
         */
        private ViewHolder(final View view)
        {
            super(view);
            viewCompleta = view;
            tvMissioni = view.findViewById(R.id.tvMissioni);
            tvUltimoUsato = view.findViewById(R.id.tvUltimo);
            boxContatori = view.findViewById(R.id.boxContatori);
            tvOrdiniCarico = view.findViewById(R.id.tvOrdiniCarico);
            tvRagioneSociale = view.findViewById(R.id.tvRagioneSociale);
            tvInfoSecondaria = view.findViewById(R.id.tvInfoSecondaria);
            tvOrdiniSpedizione = view.findViewById(R.id.tvOrdiniSpedizione);
            tvOrdiniProduzione = view.findViewById(R.id.tvOrdiniProduzione);
            tvOrdiniTrasferimento = view.findViewById(R.id.tvOrdiniTrasferimento);
        }
    }

how should i create my view if not like this? I also checked the android manual, and the recommended way is this.

final expected result: no strict violation detected!

1

There are 1 answers

0
bongo On BEST ANSWER

I think that your code is perfectly fine and nothing can be optimized, at the end there are some cases in which some data needs be loaded by the system and it's perfectly ok. There is also a documentation note about that:

But don't feel compelled to fix everything that StrictMode finds.In particular, many cases of disk access are often necessary during the normal activity lifecycle. Use StrictMode to find things you did by accident. Network requests on the UI thread are almost always a problem, though.

https://developer.android.com/reference/android/os/StrictMode