Java – How to create an interface to get information from a fragment to Android Activity?

How to create an interface to get information from a fragment to Android Activity?… here is a solution to the problem.

How to create an interface to get information from a fragment to Android Activity?

I’ve been desperately trying to build an Android app using a simple fragment (which I’ve used twice) for the past few days. I want to pass the contents of the fragment’s EditText box to the new activity. I just don’t know how to get these from fragments. I’ve been like this so far :

I have my edit_text_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <EditText 
        android:id="@+id/my_edit_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="my hint" />
</LinearLayout>

and the corresponding MyEditTextFragment.java:

public class MyEditTextFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.edit_text_fragment, container, false);
        return view;
    }
}

Then I used this fragment twice in my main.xml as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <fragment 
        android:id="@+id/detailfragment_placeholder"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        class="com.example.fragmenttester5.MyEditTextFragment" />
    <fragment 
        android:id="@+id/detailfragment_placeholder2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        class="com.example.fragmenttester5.MyEditTextFragment" />
    <Button 
        android:id="@+id/submit_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit all of it" />
</LinearLayout>

In my MainActivity, I connect the button to a new Activity :

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

Button submitButton = (Button) findViewById(R.id.submit_button);
        submitButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v){
                Intent intent = new Intent(MainActivity.this, OtherActivity.class);
                intent.putExtra("result1", "the_result_from_the_first_editText");
                intent.putExtra("result2", "the_result_from_the_second_editText");
                startActivity(intent);
            }
        });
    }
}

I

think I need to define some kind of interface in the fragment now, but I can’t find a way. I’ve read some examples and tutorials (like this one), but they don’t make sense to me. I don’t understand the given code, I just don’t understand how to adapt it to my use case.

So my question; Who can help me get the fragment’s content from the activity? The example would be very popular because I’m just banging my head against the wall here…

Solution

You’re right, this is a standard way to pass data from a fragment to an activity.

Basically, you define a listener interface implemented by an activity, and the activity registers itself as a listener for the fragment.

Here’s a simple example:

fragment

class MyFragment extends Fragment {

interface Listener {
        public void somethingHappenedInFragment(Object... anyDataYouWantToPassToActivity);
    }

private Listener mListener;

public void setListener(Listener listener) {
        mListener = listener;
    }

// ... your code ...

 Now here you pass the data to the activity
    mListener.somethingHappenedInFragment(some, data);

// ... more of your code
 }

Activity

public MyActivity extends Activity implements MyFragment.Listener {

// ... your code ...

 creating the Fragment
    MyFragment f = new MyFragment();

 register activity as listener
    f.setListener(this);

// ... more of your code

 implementation of MyFragment.Listener interface
    @Override
    public void somethingHappenedInFragment(Object... anyDataYouWantToPassToActivity) {
         here you have the data passed from the fragment.
        for (Object o : anyDataYouWantToPassToActivity {
            System.out.println(o.toString();
        }
    }

}

Related Problems and Solutions