title: Fragment: Get object from activity (Tab View)
body: <p>how can I can access in this example the object "selectedBox" from the fragment? I need it in the main activity for testing connection and in the fragment.</p>
  
  <p>In the moment I create it in the main and the fragment.</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>public class MainActivity extends FragmentActivity implements ActionBar.TabListener{
  
      private ViewPager viewPager;
      private ActionBar actionBar;
      private MainTabAdapter mAdapter;
      private String[] tabs = { "Live" };
      private box selectedBox;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          viewPager = (ViewPager) findViewById(R.id.pager);
          actionBar = getActionBar();
          mAdapter = new MainTabAdapter(getSupportFragmentManager());
          viewPager.setAdapter(mAdapter);
          actionBar.setHomeButtonEnabled(false);
          actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
          for (String tab_name : tabs) {
              actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
          }
  
          viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
              @Override
              public void onPageSelected(int position) {
                  actionBar.setSelectedNavigationItem(position);
              }
              @Override
              public void onPageScrolled(int arg0, float arg1, int arg2) {
              }
              @Override
              public void onPageScrollStateChanged(int arg0) {
              }
          });
  
          selectedBox = new box(this);
      }
  
      public void onResume(){
          super.onResume();
          if(selectedBox.getId() != -1){
              checkReachable(selectedBox.getIpPort());
          }else {
              Intent intent = new Intent(this, SettingsActivity.class);
              this.startActivity(intent);
              finish();
          }
      }
  
      private void checkReachable(String ipPort) {
          Log.d("XML Request", "CHECK BOX");
          String e2About = "http://" + ipPort + "/web/about";
          getXml.DownloadCompleteListener dcl = new getXml.DownloadCompleteListener() {
              @Override
              public void onDownloadComplete(String result) {
                  if (!result.contains("&lt;e2enigmaversion&gt;")) {
                      Toast.makeText(getApplicationContext(), "Active box not reachable.", Toast.LENGTH_SHORT).show();
                      Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
                      startActivity(intent);
                  }else{
                      viewPager.setAdapter(mAdapter);
                  }
              }
          };
          getXml downloader = new getXml(dcl);
          downloader.execute(e2About);
      }
  
  
      @Override
      public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
      }
  
      @Override
      public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
          viewPager.setCurrentItem(tab.getPosition());
      }
  
      @Override
      public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          int id = item.getItemId();
          switch (id){
              case R.id.action_settings:
                  Intent intent = new Intent(this, SettingsActivity.class);
                  startActivity(intent);
                  return true;
          }
          return super.onOptionsItemSelected(item);
      }
  }
  </code></pre>
  
  <p>MainTabAdapter</p>
  
  <pre><code>public class MainTabAdapter extends FragmentPagerAdapter {
  
      public MainTabAdapter(FragmentManager fm){
          super(fm);
      }
      @Override
      public Fragment getItem(int index) {
          switch (index) {
              case 0:
                  return new MainFragmentLive();
          }
          return null;
      }
  
      @Override
      public int getCount() {
          return 1;
      }
  }
  </code></pre>
  
  <p>MainFragmentLive.java</p>
  
  <pre><code>public class MainFragmentLive extends ListFragment {
  
      private box selectedBox;
  
      private List&lt;String&gt; bouquetListString;
  
      private ArrayAdapter&lt;String&gt; adapter;
  
      @Override
      public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_main_live, container, false);
  
          Log.d("Position", "Main Live Set Box");
          selectedBox = new box(getActivity());
          bouquetListString = selectedBox.getBouquets().get(2);
          if(bouquetListString.size() &lt; 1){
              getBouquetBox();
          }
          adapter = new ArrayAdapter&lt;String&gt;(getActivity(),android.R.layout.simple_list_item_1, bouquetListString);
          setListAdapter(adapter);
  
          ImageButton reloadBouquet = (ImageButton) view.findViewById(R.id.reloadBouquet);
          reloadBouquet.setOnClickListener(new View.OnClickListener(){
              @Override
              public void onClick(View arg0) {
                  getBouquetBox();
              }
          });
  
          setHasOptionsMenu(true);
          return view;
      }
  
  
      public void onListItemClick(ListView l, View v, int position, long id) {
          SharedPreferences savedData = getActivity().getSharedPreferences("box",0);
          SharedPreferences.Editor editor = savedData.edit();
          editor.putInt("boId", position);
          editor.apply();
          Intent gotoChannels = new Intent(getActivity(), ChannelListActivity.class);
          startActivity(gotoChannels);
      }
  
      public void getBouquetBox(){
          getXml.DownloadCompleteListener dcl = new getXml.DownloadCompleteListener() {
              @Override
              public void onDownloadComplete(String result) {
                  selectedBox.delBouquets();
                  bouquetListString.clear();
                  String [] tags = {"e2servicereference", "e2servicename"};
                  List&lt;List&lt;String&gt;&gt; bouquetsList = parseXml.parseXmlByTag(result, tags);
                  selectedBox.addBouquets(bouquetsList);
                  bouquetListString.addAll(selectedBox.getBouquets().get(2));
                  adapter.notifyDataSetChanged();
              }
          };
          Log.d("XML Request", "GET BOUQUET");
          getXml downloader = new getXml(dcl);
          downloader.execute("http://" + selectedBox.getIpPort() + "/web/getservices");
      }
  }
  </code></pre>
  
  <p>activity_main.xml</p>
  
  <pre><code>&lt;android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/pager"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  &lt;/android.support.v4.view.ViewPager&gt;
  </code></pre>
  
  <p>fragment_main_live.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:layout_margin="8dp"&gt;
          &lt;TextView
              android:layout_width="0dip"
              android:layout_height="wrap_content"
              android:layout_weight="0.8"
              android:text="@string/selectBouquet"
              style="@style/header1"/&gt;
          &lt;ImageButton
              android:layout_width="0dip"
              android:layout_height="wrap_content"
              android:id="@+id/reloadBouquet"
              android:src="@drawable/ic_action_refresh"
              android:contentDescription="@string/search"
              android:layout_weight=".20"
              android:layout_gravity="bottom"/&gt;
      &lt;/LinearLayout&gt;
      &lt;View
          android:layout_width="fill_parent"
          android:layout_height="1dp"
          android:background="@android:color/darker_gray"/&gt;
      &lt;ListView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@android:id/list" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>