4

I want to call a method of FragmentB (Class) from a fragmentA I tried by making a object of fragmentb in fragmentA (class) but it's not working here is the code of fragmentA in this class I have a method through which I will call the method of FragmentB class

adddata.setOnClickListener(
        new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean isInserted = myDb.addalldata(monthly_income.getText().toString(),
                room_rent.getText().toString(),
                mess_rent.getText().toString());
            if (isInserted = true)
                Toast.makeText(getActivity().getBaseContext(), "Data Inserted", Toast.LENGTH_LONG).show();
            else
            Toast.makeText(getActivity().getBaseContext(), "Data not Inserted", Toast.LENGTH_LONG).show();
        }
    }
);

I want to call this method of fragmentB

public void show() {
    Cursor res = myDb.getAllData();
    StringBuffer buffer = new StringBuffer();
    while (res.moveToNext()) {
        displayresult.setText( buffer.append( res.getString(1)));
    }
}

I tried by writing this code in method of fragmentA but am getting error

FragmentA fragment=          
    (FragmentA)getSupportFragmentManager().findFragmentById(R.id.pageview2);
    ((FragmentA)fragment).show();
Rick Smith
  • 9,031
  • 15
  • 81
  • 85
robinHood013
  • 209
  • 1
  • 2
  • 15

3 Answers3

13

Try this solution:

((FragmentA) getActivity()
    .getSupportFragmentManager()
    .findFragmentById(R.id.pageview2)
).show();
Boris Schegolev
  • 3,601
  • 5
  • 21
  • 34
  • i wanna call method of fragmentB so i used FragmentB , in ur code but wheni clicked my app stopped working – robinHood013 Aug 19 '15 at 16:14
  • can you post the crash report that is shown in the logcat – Vindhya Pratap Singh Aug 19 '15 at 16:19
  • com.awan.app.hostelbudgetfriend.FragmentA$1.onClick(FragmentA.java:60) at android.view.View.performClick(View.java:4753) at android.view.View$PerformClick.run(View.java:19567) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5635) at java.lang.reflect.Method.invokeNative(Native Method) – robinHood013 Aug 19 '15 at 16:24
  • com.awan.app.hostelbudgetfriend.FragmentA$1.onClick(FragmentA.java:60) at just above you first line you will get the exact exception is it Null pointer exception? – Vindhya Pratap Singh Aug 19 '15 at 16:26
  • Try to see what exactly is written in line number 60 of your FragmentA class code if there is any component i.e. used is not defined like button or other then it will create a nullpointer exception or post the code. Or try to find out in log cat it shows where exactly error is occuring – Vindhya Pratap Singh Aug 19 '15 at 16:33
  • at line 60 the code which u gave it to me , is written – robinHood013 Aug 19 '15 at 16:35
  • it means either R.id.pageview2 is not defined on the layout wherer you defined you fragment or Your FragmentA is detached from the activity and you are try to call its method which is null. Try to study about fragment lifecycle. You can check this by making your code like this FragmentA fragA = (FragmentA)getActivity().getSupportFragmentManager().findFragmentById(R.id.pageview2); if(fragA != null){ fragA.show(); } this time it will not crash but did not call show method if it is null – Vindhya Pratap Singh Aug 19 '15 at 16:40
4

You can create static varibales like this

static  FragmentB f;

public static FragmentB newInstance(String title) {
        FragmentB f = new FragmentB();
        Bundle b = new Bundle();
        b.putString(ARG_STATION_TITLE, title);
        f.setArguments(b);
        return f;
    }

You can use the getInstance() method to get the instance of fragmentB

public static FragmentB getInstance(){
    return f;
}

Call like this FragmentB.getInstance().methodinFragmentB();

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Sunil Sunny
  • 3,949
  • 4
  • 23
  • 53
  • can u explain where i have to write this code , imean in some method ? – robinHood013 Aug 19 '15 at 16:08
  • can u show where your fragmentTransaction takes place from fragmenta to fragmentb – Sunil Sunny Aug 19 '15 at 16:18
  • actually in fragmentA i take some values , and in fragment b am showing those values , so for showing updating db values in fragment b i call a method of fragmentB (so automatically when user add some values in fragmentA it automatically shows updated values in fragmentB) – robinHood013 Aug 19 '15 at 16:21
3

In FragmentA class you can do the following code:-

private static FragmentA instance = null;

@Override  
public void onCreate(@Nullable Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);  
instance = this;  
}

public static FragmentA getInstance() {  
return instance;  
}

And in FragmentB class you can call the method as follows:

FragmentA.getInstance().show(); 
Rakesh Kumar
  • 335
  • 3
  • 7