title: Android how to send an Intent from a fragment when a RecyclerView item is clicked
body: <p>My app is coded with a RecyclerView that is contained within a fragment. I'm able to successfully populate the RecyclerView with a list of mock data, however I don't how how to send an Intent to another Activity when one of the items is clicked. When I try to call <code>StartActivity</code> from within my fragment, Android Studio cannot resolve the method. </p>
  
  <p>My <code>onItemClick</code> is implemented via an interface in my viewholder class. The interface also generates an error <code>inner classes cannot have static declarations</code>. 
  I'm wondering if I'm unable to call <code>StartActivity</code> from the fragment because neither my RecyclerView adapter or viewholder classes extend <code>Activity</code>. </p>
  
  <p>What the appropriate implementation to send an Intent when an item in a RecyclerView is clicked? </p>
  
  <p><strong>PlaceHolderFragment</strong> </p>
  
  <pre><code>public static class PlaceholderFragment extends Fragment {
  
          Activity mActivity;
          RecyclerView mRecyclerView;
          TaskAdapter taskAdapter;
  
          public PlaceholderFragment() {
          }
  
          @Override
          public void onAttach(Activity activity) {
              super.onAttach(activity);
              this.mActivity = (Activity) activity;
              setRetainInstance(true);
          }
  
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {
  
              View rootView = inflater.inflate(R.layout.fragment_main, container, false);
              mRecyclerView = (RecyclerView) rootView.findViewById(R.id.cardList);
              taskAdapter = new TaskAdapter(createList(30));
              return rootView;
          }
  
          @Override
          public void onViewCreated(View view, Bundle savedInstanceState) {
              super.onViewCreated(view, savedInstanceState);
              mRecyclerView.setAdapter(taskAdapter);
              mRecyclerView.setHasFixedSize(true);
              mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
              mRecyclerView.setItemAnimator(new DefaultItemAnimator());
  
              //Cannot resolve method 
              taskAdapter.SetItemClickListener(new TaskAdapter.TaskViewHolder.OnItemClickListener() {
  
                  @Override
                  public void onItemClick(View v, int position) {
                      Intent detailIntent = new Intent(v.getContext(), DetailActivity.class);
                      //Cannot resolve method 
                      StartActivity(detailIntent);
  
                  }
              });
  
          }
  
          private List&lt;Task&gt; createList(int size) {
              List&lt;Task&gt; result = new ArrayList&lt;&gt;();
              for (int i = 1; i&lt;=size; i++) {
                  Task task = new Task();
                  task.title = Task.TITLE_PREFIX + i;
                  task.updated = Task.UPDATED_PREFIX +i;
                  task.notes = task.NOTES_PREFIX + i;
  
                  result.add(task);
  
              }
              return result;
          }
      }
  </code></pre>
  
  <p><strong>TaskAdapter</strong> </p>
  
  <pre><code>public class TaskAdapter extends RecyclerView.Adapter&lt;TaskAdapter.TaskViewHolder&gt; {
  
      private List&lt;Task&gt; taskList;
      TaskViewHolder.OnItemClickListener mItemClickListener;
  
      public TaskAdapter(List&lt;Task&gt; taskList) {
          this.taskList = taskList;
      }
  
      @Override
      public int getItemCount() {
          return taskList.size();
      }
  
      @Override
      public void onBindViewHolder(TaskViewHolder taskViewHolder, int i) {
          Task task = taskList.get(i);
          taskViewHolder.mTitle.setText(task.title);
          taskViewHolder.mUpdated.setText(task.updated);
          taskViewHolder.mStatus.setChecked(false);
      }
  
      @Override
      public TaskViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
  
          View itemView = LayoutInflater.from(viewGroup.getContext()).
                  inflate(R.layout.tasklist_layout, viewGroup, false);
  
          return new TaskViewHolder(itemView);
      }
  
      public class TaskViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
  
          protected TextView mTitle;
          protected TextView mUpdated;
          protected CheckBox mStatus;
  
          public TaskViewHolder(View view) {
              super(view);
              mTitle = (TextView) view.findViewById(R.id.title);
              mUpdated = (TextView) view.findViewById(R.id.updated);
              mStatus = (CheckBox) view.findViewById(R.id.status);
              view.setOnClickListener(this);
          }
  
          @Override
          public void onClick(View v) {
              if (mItemClickListener != null) {
                  mItemClickListener.onItemClick(v, getPosition());
              }
          }
          //Inner Classes cannot have static declarations
          public interface OnItemClickListener {
              void onItemClick(View view , int position);
          }
  
          public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
              //cannot resolve symbol 
              this.mItemClickListener = mItemClickListener;
          }
      }
  }
  </code></pre>