how to call method of one fragment from another fragment class in android


how to call method of one fragment from another fragment class in android



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();




3 Answers
3



Try this solution:


((FragmentA) getActivity()
.getSupportFragmentManager()
.findFragmentById(R.id.pageview2)
).show();





i wanna call method of fragmentB so i used FragmentB , in ur code but wheni clicked my app stopped working
– Arslan Ali Awan
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)
– Arslan Ali Awan
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





yes exactly , null pointer exception
– Arslan Ali Awan
Aug 19 '15 at 16:28



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();


FragmentB.getInstance().methodinFragmentB();





can u explain where i have to write this code , imean in some method ?
– Arslan Ali Awan
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)
– Arslan Ali Awan
Aug 19 '15 at 16:21



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();





How does this answer give a better solution to the original question?
– Giovanni Lovato
Jun 28 at 16:19






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift