title: Data passing between multiple fragment and activity: send different data to activity from multiple fragments
body: <p>I have two activities, MainActivity, SecondActivity. Main Activity has a button. When I press the button Second Activity starts. Second Activity has 2 fragments, FragmentA and FragmentB. Fragment A has a button (B also), and I want to pass a string from Fragment A or B to Main Activity by clicking the button (Closing second activity).</p>
  <p>I have used some references to do my job - When I click the button of Fragment A, second activity closes and a string passed to Main activity. It works But When I click the  button of Fragment B (doing the same code as for Fragment A), I got an error,</p>
  <p><strong>java.lang.ClassCastException: (packagename).SecondActivity cannot be cast to (packagename).FragmentB$OnDataPass</strong></p>
  <p><em>dataflow:</em></p>
  <blockquote>
  <p>Fragment A (button click) &gt;&gt; Main Activity (text view) (works)</p>
  <p>Fragment B (button click) &gt;&gt; Main Activity (text view) (does not work)</p>
  </blockquote>
  <p>Here is my Fragment A code:</p>
  <pre><code>String data =&quot;String from fragment A&quot;;
  
  public interface OnDataPass {
      public void onDataPass(String data);
  }
  
  OnDataPass dataPasser;
  
  @Override
  public void onAttach(Activity a) {
      super.onAttach(a);
      dataPasser = (OnDataPass) a;
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      
      view = inflater.inflate(R.layout.fragment_a, container, false);
      btn = (Button) view.findViewById(R.id.btn1);
      
      btn.setOnClickListener(new View.OnClickListener() {
          
          @Override
          public void onClick(View arg0) {
              // TODO Auto-generated method stub
              dataPasser.onDataPass(data);
              getActivity().finish();
          }
      });
      
      
      return view;
  }
  
  public void passData(String data) {
      dataPasser.onDataPass(data);
  }
  </code></pre>
  <p>Here is my Fragment B code:</p>
  <pre><code>String data =&quot;String from fragment B&quot;;
  
  public interface OnDataPass {
      public void onDataPass(String data);
  }
  
  OnDataPass dataPasser;
  
  @Override
  public void onAttach(Activity a) {
      super.onAttach(a);
      dataPasser = (OnDataPass) a; // Exception occurs here
  
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      
      view = inflater.inflate(R.layout.fragment_b, container, false);
      btn = (Button) view.findViewById(R.id.btn1);
      
      btn.setOnClickListener(new View.OnClickListener() {
          
          @Override
          public void onClick(View arg0) {
              // TODO Auto-generated method stub
              dataPasser.onDataPass(data);
              getActivity().finish();
          }
      });
      
      
      return view;
  }
  
  public void passData(String data) {
      dataPasser.onDataPass(data);
  }
  </code></pre>
  <p>My second Activity code:</p>
  <pre><code>public class SecondActivity extends FragmentActivity implements OnDataPass {
  
      Button btn1,btn2;   
      TabHost tabHost;
      String dt=&quot;&quot;;
      ViewPager viewPager = null;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          setContentView(R.layout.activity_help);
          
          viewPager = (ViewPager) findViewById(R.id.pager);
          FragmentManager fragmentManager = getSupportFragmentManager();
          viewPager.setAdapter(new MyAdapter(fragmentManager)); 
          
      }
  
      @Override
      public void onDataPass(String data) {
          // TODO Auto-generated method stub
          Log.d(&quot;LOG::&quot;,&quot;hello &quot; + data);
          dt= data;
          
          Intent intent = new Intent();
          intent.putExtra(&quot;key&quot;, dt);
          setResult(RESULT_OK, intent);
          finish();
          //startActivity(intent);
      }
  }
  
  class MyAdapter extends FragmentPagerAdapter {
  
      public MyAdapter(FragmentManager fm) {
          super(fm);
          // TODO Auto-generated constructor stub
      }
  
      @Override
      public Fragment getItem(int i) {
          // TODO Auto-generated method stub
          Fragment fragment=null;
          
          if(i==0) {
              fragment = new FragmentA();
          }
          if(i==1) {
              fragment = new FragmentB();
          }
              
          return fragment;
      }
  
      @Override
      public int getCount() {
          // TODO Auto-generated method stub
          return 2;
      }
      
      @Override
      public CharSequence getPageTitle(int position) {
          // TODO Auto-generated method stub
          if(position == 0) {
              return &quot;Tab 1&quot;;
          }
          if(position == 1) {
              return &quot;Tab 2&quot;;
          }
          return null;
      }
      
  }
  </code></pre>
  <p>What would I need to do to this code to get the string in FragmentB to MainActivity?
  Please post code.</p>