Chapter 10 - Challenge


I did the challenge without startActivityForResult, I’ve seen some solutions with this approach but I think the result is a value already known by the fragment (the clicked position) is not something created by the crime activity, therefore I saved the last clicked position in the fragment from the ChrimeHolder#onClick callback. Is there a downside with my solution?

public class CrimeListFragment extends Fragment {

    private RecyclerView crimeRecyclerView;
    private CrimeAdapter crimeAdapter;
    private int lastPositionClicked;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_crime_list, container, false);

        crimeRecyclerView = view.findViewById(;
        crimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));


        return view;

    public void onResume() {

    private void updateUI() {
        Collection<Crime> crimes = CrimeLab.INSTANCE.getCrimes();
        if (crimeAdapter == null) {
            crimeAdapter = new CrimeAdapter(crimes);
        } else {

    private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        int position;
        private Crime crime;
        private TextView txtViewTitle;
        private TextView txtViewDate;
        private ImageView imgViewCrimeSolved;

        public CrimeHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.list_item_crime, parent, false));

            txtViewTitle = itemView.findViewById(;
            txtViewDate = itemView.findViewById(;
            imgViewCrimeSolved = itemView.findViewById(;

        public void bind(Crime crime, int position) {
            this.crime = crime;
            this.position = position;
            txtViewDate.setText(DateFormat.format("dd/MM/yyyy", crime.getDate()));
            imgViewCrimeSolved.setVisibility(crime.isSolved() ? View.VISIBLE : View.GONE);

        public void onClick(View view) {
            CrimeListFragment.this.lastPositionClicked = position;
            startActivity(CrimeActivity.newIntent(getActivity(), crime.getId()));

    private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {

        private List<Crime> crimes;

        public CrimeAdapter(Collection<Crime> crimes) {
            this.crimes = new ArrayList<>(crimes);

        public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(getActivity());
            return new CrimeHolder(inflater, parent);

        public void onBindViewHolder(CrimeHolder holder, int position) {
            holder.bind(crimes.get(position), position);

        public int getItemCount() {
            return crimes.size();