0

I have an ActionBar with Tabs which is fragments and ViewPager. In the MainActivity there is implemented a LocationListener.

How can I passes the location data to the fragment from the MainActivity?

MainActivity:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener, LocationListener{

ActionBar actionbar;
ViewPager viewpager;
FragmentPageAdapter ft;
public String str="STRING";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewpager = (ViewPager) findViewById(R.id.pager);
    ft = new FragmentPageAdapter(getSupportFragmentManager());

    final RunFragment fragment = (RunFragment) ft.getItem(viewpager.getCurrentItem());
    fragment.setTv(str);

    actionbar = getActionBar();
    viewpager.setAdapter(ft);
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionbar.addTab(actionbar.newTab().setText("Run").setTabListener(this));
    actionbar.addTab(actionbar.newTab().setText("Map").setTabListener(this));
    actionbar.addTab(actionbar.newTab().setText("Statistics").setTabListener(this));
    viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
        actionbar.setSelectedNavigationItem(arg0);

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }
    });


}

I want to write out the location data in the setTv() method the Tab's code:

public class RunFragment extends Fragment {

public TextView tv;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.run_layout, container, false);
    tv =(TextView)view.findViewById(R.id.textView1);

    });


    return view;

}

public void setTv(String string){
    tv.setText(string);
}

}

Trung Nguyen
  • 7,442
  • 2
  • 45
  • 87
kalmi
  • 13
  • 6
  • [Check](http://developer.android.com/training/basics/fragments/communicating.html) – Skynet Jan 16 '15 at 10:01
  • You can attach a bundle, which will hold your data. – Vilas Jan 16 '15 at 10:02
  • what problem you are getting? also call `fragment.setTv(str);` after setting adapter to ViewPager. i thing `onPageSelected` method is also called when first fragment is selected so use `fragment.setTv(str);` in `onPageSelected` method – ρяσѕρєя K Jan 16 '15 at 10:04
  • Yes but if I do anything in the setTv() method the Android crashes, and I don't know why – kalmi Jan 16 '15 at 10:20

2 Answers2

1

You should consider using an event bus library, like Greenrobot EventBus. With it, you will be able to send out event objects from your activity whenever needed and the Fragment will receive them only when it is alive.

Kelevandos
  • 7,024
  • 2
  • 29
  • 46
0

u can do smthing like

// map is a GoogleMap object
map.setMyLocationEnabled(true);

check this link

kevz
  • 2,727
  • 14
  • 39