title: Passing spinner value from activity to fragment
body: <p>I have a custom spinner with an ArrayList defined in an activity. On selected item a new fragment is created. Can I pass the value of which fragment was created ("Single Massage" or "Couples Massage") to another fragment within the same activity and then from that fragment pass it to another activity and update its textview? This might not be the best approach, but I am a beginner, so I really don't know that much. Here is my code.</p>
  
  <p>Spinner:</p>
  
  <pre><code>public class MassageActivity extends AppCompatActivity {
  private Spinner mSpinner;
  private Toolbar mToolbar;
  private FragmentManager mFragmentManager;
  private FragmentTransaction mFragmentTransaction;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_massage);
      mToolbar = (Toolbar) findViewById(R.id.toolbar_massage);
      setSupportActionBar(mToolbar);
      ActionBar ab = getSupportActionBar();
      if (ab != null) {
          ab.setDisplayHomeAsUpEnabled(true);
      }
      if (ab != null) {
          ab.setDisplayShowTitleEnabled(false);
      }
      customSpinner();
  
      mFragmentManager = getSupportFragmentManager();
  }private void customSpinner() {
      final ArrayList&lt;String&gt; massageType = new ArrayList&lt;&gt;();
      massageType.add("Single Massage");
      massageType.add("Couples Massage");
      massageType.add("Pamper Party");
      mSpinner = (Spinner) findViewById(R.id.spinner);
  
      CustomSpinnerAdapter spinnerAdapter = new CustomSpinnerAdapter(getApplicationContext(), massageType);
      mSpinner.setAdapter(spinnerAdapter);
  
      mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
              if (parent.getItemAtPosition(position).toString()
                      .equals("Pamper Party")) {
                  mFragmentTransaction = mFragmentManager.beginTransaction();
                  mFragmentTransaction.replace(R.id.fragment_container_massage,
                          new PamperPartyFragment()).commit();
              }
              if (parent.getItemAtPosition(position).toString()
                      .equals("Single Massage")) {
                  mFragmentTransaction = mFragmentManager.beginTransaction();
                  mFragmentTransaction.replace(R.id.fragment_container_massage,
                          new SingleMassageFragment()).commit();
              }
              if  (parent.getItemAtPosition(position).toString()
                      .equals("Couples Massage")){
                  mFragmentTransaction = mFragmentManager.beginTransaction();
                  mFragmentTransaction.replace(R.id.fragment_container_massage,
                          new CouplesMassageFragment()).commit();
              }
  
          }
  
          @Override
          public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  
          }
      });
  
  }}
  </code></pre>
  
  <p>EDIT:
  I followed the and done this:</p>
  
  <pre><code>String spinnerValue = mSpinner.getSelectedItem().toString();
      Intent i = new Intent(this, SingleMassageFragment.class);
      i.putExtra("spinnerValue", spinnerValue);
      setIntent(i);
  </code></pre>
  
  <p>And then in my fragment:</p>
  
  <pre><code>MassageActivity activity = (MassageActivity) getActivity();
      Intent f = activity.getIntent();
      String spinnerValue = f.getStringExtra("spinnerValue");
      Utils.doToast(getContext(), spinnerValue);
  </code></pre>
  
  <p>But whichever fragment I selected, I always get the same toast "Single Massage". Why is that? For any case here's my adapter:</p>
  
  <pre><code>public class CustomSpinnerAdapter extends BaseAdapter {
  private Context mContext;
  private ArrayList&lt;String&gt; mMassageType;
  private LayoutInflater mInflater;
  private TextView tvSpinner;
  
  public CustomSpinnerAdapter(Context context, ArrayList&lt;String&gt; massageType) {
      mContext = context;
      mMassageType = massageType;
      mInflater = (LayoutInflater.from(context));
  }
  
  @Override
  public int getCount() {
      return mMassageType.size();
  }
  
  @Override
  public Object getItem(int position) {
      return mMassageType.get(position);
  }
  
  @Override
  public long getItemId(int position) {
      return (long) position;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      convertView = mInflater.inflate(R.layout.spinner_layout, null);
      tvSpinner = (TextView) convertView.findViewById(R.id.tvSpinner);
      tvSpinner.setText(mMassageType.get(position));
      return convertView;
  }
  </code></pre>
  
  <p>}</p>