Calling a Fragment Method from a Parent Activity

Calling a Fragment method from a parent Activity

not get the question exactly as it is too simple :

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
fragment.<specific_function_name>();

How can Call a Fragment Method from a Parent Activity in Android

The simplest way is to store locally in your activity reference to your fragment and call getToast on it:

SampleFragment myFragment; // declared in your activity class

// This is small change in your setupViewPager
myFragment = new SampleFragment ();
adapter.addFrag(myFragment, context.getResources().getString(R.string.info));

//later on when you want to call getToast:
myFragment.getToast();

You may also skip storing SampleFragment as a data member of your activity and retrievie its reference directly from FragmentManager when its needed.

Can't call Fragment method in parent Activity

commit() is asynchronous. This is why your project is crashing upon launch. Instead of using commit(), use commitNow(). Also, instead of using new TestFragment(), create a variable so you can call its methods.

TestFragment testFragment= new TestFragment();

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView, testFragment).commitNow();

testFragment.testMethod();

Kotlin: Call fragment method from parent activity

Move your fragmethod into companion object of the Fragment.

companion object {
fun fragmethod() {
// code
}
}

Calling Fragment method from Parent Activity

@Override
public void onTabUnselected(TabLayout.Tab tab) {
if (tab.getText().toString().equalsIgnoreCase("owner")) {
OwnerForm frag = (OwnerForm) getSupportFragmentManager()
.getFragments()
.get(0);
bundle = frag.getData();
}

Log.i(TAG, tab.getText().toString() + " says hi"); //test statement
}

Using getFragments().get() did the trick for me, I didn't know FragmentManager keeps a list of all the active Fragments so you don't have to use getFragmentById() which causes the null object error.

Trying to call a fragment method from parent activity but the method is not being resolved

Use callback to communicate from fragment to Activity.

public interface UserAction {
void showButton();
}

In the Fragment implement the Interface

@Override
public void showButton() {
sup.setEnabled(true);
}

Then in your activity just call

UserAction mUserAction = getFragmentManager().findFragmentById(R.id.fragment);
mUserAction.showButton();

Call an activity method from a fragment

From fragment to activty:

((YourActivityClassName)getActivity()).yourPublicMethod();

From activity to fragment:

FragmentManager fm = getSupportFragmentManager();

//if you added fragment via layout xml
YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentById(R.id.your_fragment_id);
fragment.yourPublicMethod();

If you added fragment via code and used a tag string when you added your fragment, use findFragmentByTag instead:

YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentByTag("yourTag");


Related Topics



Leave a reply



Submit