title: How to send data from Activity to Fragment
body: <p>I know there are many topics about this here. I have also read documentation many times but I can't find the best way to pass data from activity to fragment.</p>
  
  <p>I want to be able to show the results of my Searchable activity in two differents layouts (list and map) using <a href="http://developer.android.com/training/implementing-navigation/lateral.html" rel="noreferrer">swipe Views with tabs</a>. I have to pass 2 data to the fragments: "currentLocation" which is the current user location and "result" which is a list of objects.</p>
  
  <p>I have omited some parts of my code to make it more understandable.</p>
  
  <p>SearchableActivity.java</p>
  
  <pre class="lang-java prettyprint-override"><code>public class SearchableActivity extends ActionBarActivity implements TabListener {
  
      List&lt;PlaceModel&gt; result = new ArrayList&lt;PlaceModel&gt;();
      private SearchView mSearchView;
      private String currentLocation;
  
      AppSectionsPagerAdapter mAppSectionsPagerAdapter;
          ViewPager mViewPager;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.activity_searchable);
  
      final ActionBar actionBar = getSupportActionBar();
      actionBar.setHomeButtonEnabled(true);
      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  
      mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
  
      mViewPager = (ViewPager) findViewById(R.id.pager);
      mViewPager.setAdapter(mAppSectionsPagerAdapter);
      mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
          @Override
          public void onPageSelected(int position) {
              actionBar.setSelectedNavigationItem(position);
          }
      });
  
      actionBar.addTab(actionBar.newTab().setText("List").setTabListener(this));
      actionBar.addTab(actionBar.newTab().setText("Map").setTabListener(this));
  
      // get currentLocation here
  
      handleIntent(getIntent());
  }
  
  @Override
  protected void onNewIntent(Intent intent) {
      handleIntent(intent);
  }
  
  private void handleIntent(Intent intent) {
  
      if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
  
          final String query = intent.getStringExtra(SearchManager.QUERY);
  
          // get result here
      }
  }
  
  @Override
  public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
      // TODO Auto-generated method stub
  
  }
  
  @Override
  public void onTabSelected(Tab tab, FragmentTransaction arg1) {
      mViewPager.setCurrentItem(tab.getPosition());
  }
  
  @Override
  public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
      // TODO Auto-generated method stub
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>PlaceListFragment.java</p>
  
  <pre class="lang-java prettyprint-override"><code>public class PlaceListFragment extends Fragment {
      ListView listViewData;
      PlaceAdapter placeAdapter;
      List&lt;PlaceModel&gt; result = new ArrayList&lt;PlaceModel&gt;();
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_list, container, false);
          Bundle args = getArguments();
          listViewData = (ListView) rootView.findViewById(android.R.id.list);
          // I will pass result and currentLocation here
          placeAdapter = new PlaceAdapter(getActivity(), R.layout.fragment_list_item, result, currentLocation);
          listViewData.setAdapter(placeAdapter);
          return rootView;
      }
  }
  </code></pre>
  
  <p>AppSectionsPagerAdapter.java</p>
  
  <pre class="lang-java prettyprint-override"><code>public class AppSectionsPagerAdapter extends FragmentPagerAdapter {
  
  final int PAGE_COUNT = 2;
  
  public AppSectionsPagerAdapter(FragmentManager fm) {
      super(fm);
  }
  
  @Override
  public Fragment getItem(int arg0) {
      Bundle data = new Bundle();
      switch(arg0) {
          case 0:
              PlaceListFragment fragment1 = new PlaceListFragment();
              fragment1.setArguments(data);
              return fragment1;
          default:
              PlaceListFragment fragment2 = new PlaceListFragment();
              fragment2.setArguments(data);
              return fragment2;
      }
  }
  
  @Override
  public int getCount() {
      return PAGE_COUNT;
  }
  }
  </code></pre>