-2

I want to call the function Ask() of MainActivity from fragment2. How can I call a function of MainActivity from fragment2? I want to import results into a page called from fragment2.

Edit: I already see that discussion, but don't have the solution of my problem.

Question
  • 145
  • 2
  • 12
  • Possible duplicate of [Call an activity method from a fragment](http://stackoverflow.com/questions/12659747/call-an-activity-method-from-a-fragment) – Daniel Nugent Oct 01 '16 at 03:08
  • You can call it directly using `getActivity()` and casting it to MainActivity, but using an interface is better as it decouples the Fragment and the Activity, see here: http://stackoverflow.com/a/31798511/4409409 – Daniel Nugent Oct 01 '16 at 03:09
  • Use interface to call the method in the MainActivity. – Liem Vo Oct 01 '16 at 04:49

3 Answers3

0

Make that function static, after that you can access that function in Fragment e.g. MainActivity.Ask();

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
0

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

Cheers!

O_o
  • 1,103
  • 11
  • 36
  • I had already tried this method before, but don't work with my code. – Question Oct 01 '16 at 19:40
  • @Question, write your codes down here and then we(StackOverFlow) can see/understand what's wrong with your code. – O_o Oct 02 '16 at 04:48
0

I would recommend you read this documentation.

call to function Ask() of MainActivity from fragment2.

For this you need a create a interface in your fragment2. The below code is an example from the document. You shouldn't ignore the onAttach method in your fragment as well.

public class Fragment2 extends ListFragment {
    OnCallActivityListener mCallback;

    // Container Activity must implement this interface
    public interface OnCallActivityListener {
        public void callAskMethod();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnCallActivityListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    ...
}

Now the fragment can deliver messages to the activity by calling the callAskMethod() method (or other methods in the interface) using the mCallback instance of the OnCallActivityListener interface.

For example, the following method in the fragment is called when the user clicks on a list item. The fragment uses the callback interface to deliver the event to the parent activity.

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // Send the event to the host activity
    mCallback.callAskMethod();
}

After that you should implement the interface in your host activity.

public static class MainActivity extends Activity
        implements Fragment2.OnCallActivityListener{
    ...

    public void callAskMethod() {
        // Do something here 
        ask();
    }
}

So that is it. You have called ask() method from fragment2 fragment.

I want to import results into a page called from fragment2.

The host activity can deliver messages to a fragment by capturing the Fragment instance with findFragmentById(), then directly call the fragment's public methods.

In your `MainActivity you can call send the result to the fragment.

Fragment2 fragment2 = (Fragment2) getSupportFragmentManager().findFragmentById(R.id.article_fragment);

So you have a instance value of the Fragment2 in MainActivity. So you can any public method of the fragment2 from there.

for example

fragment2.doSomething();

That's it.

ziLk
  • 3,120
  • 21
  • 45