title: RecyclerView - Get the selected position and pass the position to another activity where the same list will appear with that specific item selected
body: <p>I have declared a <code>ViewModel.class</code> for the RecyclerView an Adapter and I have parsed to the <code>MainActivity</code> and to another <code>Activity</code> so I have the same adapter for both activities.</p>
  
  <p>I can show the parsed data in both activities but the problem it is I cannot take the data of the selected item to <code>MainActivity</code> and then to set to that btnSearch for a click so then I can share between activities the data from the selected item.</p>
  
  <p>Every time when the app is open the first item is selected. What I am trying to achieve is.  </p>
  
  <ol>
  <li>Get the item position to the MainActivity so when I click for a button search the data of the selectedItem will going to <code>intent.putExtra</code> and then get the data at another Activity.</li>
  <li>If I click the second item and go to another Activity the same item will be selected.</li>
  </ol>
  
  <p>Here is what I have tried so far. </p>
  
  <p>The <code>SearchEngineAdapter.class</code></p>
  
  <pre><code>public class SearchEngineAdapter extends RecyclerView.Adapter&lt;SearchEngineAdapter.ViewHolder&gt; {
      private int selectedItem = 0;
      private static RecyclerViewClickListener itemListener;
  
      private Context context;
      ArrayList&lt;SearchEngine&gt; arrayList = new ArrayList&lt;&gt;();
  
      public SearchEngineAdapter(Context context, ArrayList&lt;SearchEngine&gt; arrayList, int selectedItem) {
          this.context = context;
          this.arrayList = arrayList;
          this.selectedItem = selectedItem;
      }
  
      @Override
      public void onBindViewHolder(@NonNull final ViewHolder holder, final int i) {
          holder.tvIcon.setImageResource(arrayList.get(i).getIcon());
          holder.tvId.setText(arrayList.get(i).getId());
          holder.tvSearchUrl.setText(arrayList.get(i).getUrl());
          final String url = holder.tvSearchUrl.getText().toString();
  
          SharedPreferences sp = context.getSharedPreferences("SavedSelected", Context.MODE_PRIVATE);
          SharedPreferences.Editor editor = sp.edit();
          editor.putInt("selected", selectedItem);
          editor.apply();
  
          sp = context.getSharedPreferences("SavedSelected", Context.MODE_PRIVATE);
          int myIntValue = sp.getInt("selected", -1);
          Log.d("Selected", "SharedPreferences" + myIntValue);
  
          if (selectedItem == i) {
              holder.tvIcon.setBackgroundColor(Color.parseColor("#30000000"));
              Intent intent = new Intent("search_engines");
              intent.putExtra("url", url);
              intent.putExtra("selected", selectedItem);
              LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
          } else {
              holder.tvIcon.setBackgroundColor(Color.parseColor("#00000000"));
          }
  
          holder.tvIcon.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent intent = new Intent("search_engines");
                  intent.putExtra("url", url);
                  int PreviousSelectedItem = selectedItem;
                  selectedItem = i;
                  intent.putExtra("selected", selectedItem);
                  holder.tvIcon.setBackgroundColor(Color.parseColor("#30000000"));
                  notifyItemChanged(PreviousSelectedItem);
                  notifyDataSetChanged();
              }
          });
      }
  
      // ... Other necessary functions.
  }
  </code></pre>
  
  <p>Now the <code>MainActivity.class</code></p>
  
  <pre><code>RecyclerView paramRecyclerView;
  SearchEngineAdapter sEngineAdapter;
  
  paramRecyclerView = findViewById(R.id.lvEngines);
  paramRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
  paramRecyclerView.setHasFixedSize(true);
  Intent intent = getIntent();
  int intValue = intent.getIntExtra("selected", 0);
  sEngineAdapter = new SearchEngineAdapter(context, arrayList, intValue);
  paramRecyclerView.setAdapter(sEngineAdapter);
  
  // Calling network APIs to populate the arrayList.
  </code></pre>
  
  <p>The <code>onResume</code> function of the <code>MainActivity</code> looks like the following.</p>
  
  <pre><code>protected void onResume() {
      super.onResume();
      searchPlugin.setText("");
      getChangeColor();
  }
  </code></pre>
  
  <p>This is the click handler defined in <code>MainActivity</code> which send me to another Activity.</p>
  
  <pre><code>btnSearch.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          String newEntry = searchPlugin.getText().toString();
          AddHistory(newEntry);
          getFragmentRefreshListener().onRefresh();
          Intent intent = new Intent(MainActivity.this, ActivitySearchEngine.class);
          intent.putExtra("url", url );
          intent.putExtra("name", newEntry);
          intent.putExtra("selected", selectedItem2);
          startActivity(intent);
      }
  });
  </code></pre>
  
  <p>And the other activity which is <code>ActivitySearchEngine.class</code></p>
  
  <pre><code>public class ActivitySearchEngine extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
  
      public String selectedName;
      public int selectedID;
      public String selectedSearchUrl;
      RecyclerView mListView;
      RecyclerView paramRecyclerView;
      SearchEngineAdapter sEngineAdapter;
  
      ArrayList&lt;SearchEngine&gt; arrayList = new ArrayList&lt;&gt;();
      final Context context = this;
      int selectedItem;
  
      @SuppressLint("SetJavaScriptEnabled")
      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_search_result);
  
          // Variables initialization
  
          // Setting up adapter
          paramRecyclerView.setAdapter(sEngineAdapter);
          sEngineAdapter.notifyDataSetChanged();
  
          // Calling network APIs to populate the arrayList here
  
          Intent receivedIntent = getIntent();
          selectedName = receivedIntent.getStringExtra("name");
          selectedID = receivedIntent.getIntExtra("id", 1); //NOTE: -1 is just the default value
          selectedSearchUrl = receivedIntent.getStringExtra("url");
  
          // Loading the url in a WebView for searching
      }
  }
  </code></pre>
  
  <p>I could share the selected item position between these two activities. However, I am not sure how to achieve the behavior of the item is being selected in the <code>RecyclerView</code> of the second activity as well. If I select another item in the <code>RecyclerView</code> of the second activity, the change should be reflected in the first (i.e. <code>MainActivity</code>) as well when I get back to it. </p>
  
  <p>Any help would be appreciated. </p>