Search code examples
androidandroid-fragmentspagerslidingtabstrip

Sliding Tab Layout Call Method


I have to ask you for a help with calling method from fragments in Sliding Tab Layout. I checked many answers but they depends on id or tags which my project doesn't include them. I use this: https://github.com/codepath/android_guides/wiki/Google-Play-Style-Tabs-using-SlidingTabLayout I have two activities (MainActivity which send two kinds of data to SecondActivity). SecondActivity includes tabs with 3 fragments. In first fragment (named Contrs) I see received data (one kind) but I want to make button in my actionbar which change results for second kinds of data. It works in activity without fragments but not with fragments. Here is the code:

MainActivity:

public class MainActivity extends Activity { ...


public void count (View view) { 
    double g, x, y;
    g = Double.parseDouble(a.getText().toString());
    x = g*8.5;
    y = g*5.5;
    Intent sendingIntent = new Intent(this,SecondActivity.class);
    double x1 = new BigDecimal(x).setScale(2,RoundingMode.HALF_UP).doubleValue();
    double y1 = new BigDecimal(y).setScale(2,RoundingMode.HALF_UP).doubleValue();
    sendingIntent.putExtra("x1", x1);
    sendingIntent.putExtra("y1", y1);
    startActivity(sendingIntent);
...}

SecondActivity:

public class SecondActivity extends ActionBarActivity {
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.results, menu);
    MenuItem item = menu.findItem(R.id.alt);
    Intent receiveIntent = this.getIntent();
    y1 = receiveIntent.getDoubleExtra("y1", y1);
    if (y1 == 0) {
          item.setVisible(false); 
        } else { 
            item.setVisible(true);
        }
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) { 
        case R.id.alt:
           **"IN HERE I WANT TO MAKE A METHOD WHICH IS IN MY FRAGMENT"**
           break;
        case R.id.info:
           Intent intset1 = new Intent(this, A.class);
           this.startActivity(intset1);
           break;
        case R.id.help:
           Intent intset2 = new Intent(this, B.class);
           this.startActivity(intset2);
           return true;
        default:
            return super.onOptionsItemSelected(item);
    }
    return false;
}

Contrs:

public class Contrs extends Fragment {
...
public boolean aa = true;
public TextView vvv;
public double x1;
public double y1;
final DecimalFormat df = new DecimalFormat("0.00");

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v =inflater.inflate(R.layout.activity_contributions,container,false);
    x1 = getActivity().getIntent().getDoubleExtra("x1", x1);
    y1 = getActivity().getIntent().getDoubleExtra("y1", y1);

    vvv = (TextView) v.findViewById(R.id.r1);
return v; 
}

public void v1 () { //  first kind of data
    vvv.setText(String.valueOf(df.format(x1)));
}
public void v2 () { // second kind of data
    vvv.setText(String.valueOf(df.format(y1)));
}

public void alt (){ // switch between kinds
    if (aa)
        v1();
    else
        v2();
    aa = !aa;
}

And this method (alt) I want to have in my SecondActivity.


Solution

  • You can use a event bus, it's the more simple

    http://square.github.io/otto/

    Register your fragment to the event

    and sent the message from the activity.

    in your case

    BusProvider.java

    public final class BusProvider {
      private static final Bus BUS = new Bus();
    
      public static Bus getInstance() {
        return BUS;
      }
    
      private BusProvider() {
        // No instances.
      }
    }
    

    ClickedItemEvent.java

    public class ClickedItemEvent { //the name of the event
    }
    

    activity :

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) { 
            case R.id.alt:
            BusProvider.getInstance().post(new ClickedItemEvent());
    

    fragment :

    public class YourFragment extends ... {
      ...
    
      @Override public void onResume() {
        super.onResume();
        BusProvider.getInstance().register(this);
      }
    
      @Override public void onPause() {
        super.onPause();
        BusProvider.getInstance().unregister(this);
      }
    
      @Subscribe public void onLocationChanged(ClickedItemEvent event) {
        //do anything you want
      }
    
      ...
    }