1

I am working in Fragment. i want to call one fragment from onClickListener. how can i do that? This is my code. from the else part i have to call one fragment..how to do that?

  chartView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SeriesSelection seriesSelection = chartView
                        .getCurrentSeriesAndPoint();

                if (seriesSelection == null) {
                    Toast.makeText(getActivity().getApplicationContext(),
                            "No chart element was clicked",
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(
                            getActivity().getApplicationContext(),
                            "Chart element data point index "
                                    + (seriesSelection.getPointIndex() + 1)
                                    + " was clicked" + " point value="
                                    + seriesSelection.getValue(),
                            Toast.LENGTH_SHORT).show();


                }
            }
        });
JK.C
  • 97
  • 2
  • 5
  • 1
    I'm sorry i don't understand what you mean with "call one fragment", you want to send a signal to an observed fragment or charge/resume it at runtime click? – phemt.latd Jan 22 '14 at 14:06
  • 1
    Can you please clarify your question? Do you want to replace one fragment with another inside your Listener? – nikis Jan 22 '14 at 14:10
  • in the above code, from the else part after the toast message,i want to call on fragment. if it is activity i can call via startactivity(intentobject). but i dont know how to call the fragmnet? – JK.C Jan 22 '14 at 14:11
  • 1
    Possible duplicate of http://stackoverflow.com/questions/21228721/how-to-replace-a-fragment-on-button-click-of-that-fragment/ – Srikanth Jan 22 '14 at 14:12
  • Do you have one Fragment per Activity? – Allan Macmillan Jan 22 '14 at 14:16
  • no this is my first tab. i want to call the second tab from this tab. all are fragments – JK.C Jan 22 '14 at 14:47

2 Answers2

3

There are many ways to replacing Fragments. but I follow the way below:

Create addFragmentMethod() like below

 public void addFragments(Fragment fragment) {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();
    ft.replace(android.R.id.tabcontent, fragment);
    ft.commit();
}

now override onAttach() like this

@Override
public void onAttach(Activity activity) {
    this.activity = (YourActivity) activity;
    myDetail = this.activity.myDetail;
    super.onAttach(activity);
    }

now just call

activity.addFragments(fragment);
Allan Macmillan
  • 1,481
  • 3
  • 18
  • 30
Imtiyaz Khalani
  • 2,037
  • 18
  • 32
0

I think what you want is to call a method in another Fragment that is already added. This is done via the Activity that both Fragments share. There's a guide here: http://developer.android.com/training/basics/fragments/communicating.html

katzoft
  • 858
  • 8
  • 10