Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 317
langsmith Avatar asked Oct 05 '17 00:10

langsmith


People also ask

How can you pass data from an activity to a fragment?

So, to pass data from the MotherActivity to such a Fragment you will need to create private Strings/Bundles above the onCreate of your Mother activity - which you can fill with the data you want to pass to the fragments, and pass them on via a method created after the onCreate (here called getMyData()).

How can we interface between fragments and activities?

To allow a Fragment to communicate up to its Activity, you can define an interface in the Fragment class and implement it within the Activity. The Fragment captures the interface implementation during its onAttach() lifecycle method and can then call the Interface methods to communicate with the Activity.

How will you pass data from one fragment to another fragment in Android using interface?

To pass data from one fragment to another Bundle will help. LifeShapeDetailsFragment fragment = new LifeShapeDetailsFragment(); // object of next fragment Bundle bundle = new Bundle(); bundle. putInt("position", id); fragment. setArguments(bundle);

How do you send data from activity to ViewModel?

Passing Data between fragments in Android using ViewModel: This is because ViewModel is tied to the activity lifecycle. To actually pass the data between fragments, we need to create a ViewModel object with an activity scope of both the fragments, initialize the ViewModel , and set the value of the LiveData object.


2 Answers

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);
like image 104
Sharath kumar Avatar answered Sep 18 '22 22:09

Sharath kumar


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);
like image 34
ישו אוהב אותך Avatar answered Sep 20 '22 22:09

ישו אוהב אותך