langsmith
langsmith

Reputation: 2546

Sending data from activity to fragment with interface listener

I'm trying to send data from an activity to a fragment. I'm not sending data from a fragment to an activity. I've got everything set up correctly other than instantiating the interface listener object in the activity.

public class Activity extends AppCompatActivity {

  private FragmentInterface fragmentInterfaceListener;

  @Override 
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // This line below is actually in a button onClick()
    fragmentInterfaceListener.sendDataMethod(dataToSend);

  }

  public interface FragmentInterface {
    void sendDataMethod(SampleData sampleData);
    }
  }

Then in the fragment, I have:

public static class CustomFragment extends Fragment implements Activity.FragmentInterface {

  @Override
  public void sendDataMethod(final SampleData sampleData) {

  }    
}

When I put a log line in the button onClick(), the log line appears when the button is clicked. No, I'm not going to put the sampleData in a fragment bundle. Yes, I need to send the data through an interface. So how do I correctly instantiate the fragmentInterfaceListener object in the Activity? Am I missing anything else in the Activity or CustomFragment?

Upvotes: 7

Views: 9699

Answers (3)

Yatish
Yatish

Reputation: 531

For sending data from Activity to Fragment we don't need an interface.

You can directly call the method in Fragment or pass as setArguments in Bundle

     ArticleFragment articleFrag = (ArticleFragment)
            getSupportFragmentManager().findFragmentById(R.id.article_fragment);

    if (articleFrag != null) {
        // If article frag is available, we're in two-pane layout...

        // Call a method in the ArticleFragment to update its content
        articleFrag.updateArticleView(position);
    } else {
        // Otherwise, we're in the one-pane layout and must swap frags...

        // Create fragment and give it an argument for the selected article
        ArticleFragment newFragment = new ArticleFragment();
        Bundle args = new Bundle();
        args.putInt(ArticleFragment.ARG_POSITION, position);
        newFragment.setArguments(args);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();
    }

You can refer https://developer.android.com/training/basics/fragments/communicating.html

Upvotes: 1

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29783

You don't need to use listener in the Fragment because you can directly communicate with the Fragment from its host Activity.

As @lq-gioan says, you can create a public method in your Fragment then call it from your activity. So, create a public method to set the data, something like this:

public static class CustomFragment extends Fragment {

  // public method to be accessed by host activity.
  public void sendDataMethod(final SampleData sampleData) {

  }    
}

Then you can call the method within your host activity:

CustomFragment fragment = (CustomFragment)getSupportFragmentManager()
                           .findFragmentById(R.id.fragment_id);

// or use find by tag if you adding the fragment by tag
// CustomFragment fragment = (CustomFragment)getSupportFragmentManager()
//                           .findFragmentByTag("FragmentTag");

// now you can call it
fragment.sendDataMethod(yourSampleData);

Upvotes: 3

Sharath kumar
Sharath kumar

Reputation: 4132

What your are missing here is the registering part.

The fragment has to register itself with the activity listener for the activity to send data when an event occurs.To do this create a method in activity

private void setOnDataListener(FragmentInterface interface){
    fragmentInterfaceListener=interface;
}

And in the oncreate of your fragment set the listener like this

((YOUR_ACTIVITY_NAME)getActivity()).setOnDataListener(this);

Upvotes: 14

Related Questions