How to create an interface to get info from a Fragment to an Android Activity?

孤者浪人 提交于 2019-12-03 17:34:47

So you'd create an Interface to talk with the Activity if the event happened in the Fragment. For situations like this, you can simply make an accessible method in the Fragment that the Activity can call. So

public class MyEditTextFragment extends Fragment {
    private EditText mEditText;

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

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mEditText = (EditText) getView().findViewById(R.id.my_edit_text);
    }

    public Editable getText() {
        return mEditText.getText();
    }
}

Then

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

        final MyEditTextFragment fragment1 = (MyEditTextFragment)
            getFragmentManager().findFragmentById(R.id.detailfragment_placeholder);

        final MyEditTextFragment fragment2 = (MyEditTextFragment)
            getFragmentManager().findFragmentById(R.id.detailfragment_placeholder2);

        Button submitButton = (Button) findViewById(R.id.submit_button);
        submitButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v){

                String firstResult = fragment1.getText().toString();
                String secondResult = fragment2.getText().toString();

                Intent intent = new Intent(MainActivity.this, OtherActivity.class);
                intent.putExtra("result1", firstResult);
                intent.putExtra("result2", secondResult);
                startActivity(intent);
            }
        });
    }
}

This assumes that you assigned the Fragment tags in your FragmentTransaction. Be sure to check for null Fragments (omitted for brevity)

You are right, that's kind of a standard way to pass data from a Fragment to an activity.

Basically you define a Listener interface which the Activity implements, and the Activity registers itself as a Listener with 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();
        }
    }

}

On a high level, there are two tasks that you commonly need to solve with Fragments. The first is communicating data from an Activity to a Fragment. The second is communicating data from a Fragment to an Activity.

An Activity knows which Fragments it contains since it creates them, so it's easy to communicate that way - just call methods on the Fragment itself. But the inverse is not true; Fragments might be attached to any number of random Activities, so it doesn't know anything about it's parent.

The solution is to implement an interface that the Activity implements and the Fragment knows how to communicate with. That way, your Fragment has something it knows how to talk with. There are specific code examples for how to do it here: http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

(In particular, check out the "Creating event callbacks to the activity" code examples).

Albert Nguyen

Activity will be received data from updateDetail() method in Fragment

//// Activity 
public class RssfeedActivity extends Activity implements MyListFragment.OnItemSelectedListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rssfeed);
        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("Annv - Fragment", "onClick here");
            }
        });
    }

    // if the wizard generated an onCreateOptionsMenu you can delete
    // it, not needed for this tutorial

    @Override
    public void onRssItemSelected(String link) {
        //        DetailFragment fragment = (DetailFragment) getFragmentManager()
        //                .findFragmentById(R.id.detailFragment);
        //        if (fragment != null && fragment.isInLayout()) {
        //          fragment.setText(link);
        //        } 
        //        Intent start = new Intent(this, RssfeedSecondActivity.class);
        //        startActivity(start);
        DetailFragment fragment = (DetailFragment) getFragmentManager()
                .findFragmentById(R.id.detailFragment);
        if (fragment != null && fragment.isInLayout()) {
            fragment.setText(link);
        }
    }

}
/// Fragment
public class MyListFragment extends Fragment {

    private OnItemSelectedListener listener;
    private OnItemStartActivityListener listenerStartAct;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_rsslist_overview,
                container, false);
        Button button = (Button) view.findViewById(R.id.button1);
        Log.d("Annv - Fragment", "run on " + getActivity().toString());
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateDetail();
            }
        });
        return view;
    }

    public interface OnItemSelectedListener {
        public void onRssItemSelected(String link);
    }

    public interface OnItemStartActivityListener {
        public void onRssStartActivity(String link);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof OnItemSelectedListener) {
            Log.d("Annv - Fragment", "activity " + activity.getLocalClassName());
            listener = (OnItemSelectedListener) activity;
        } else if (activity instanceof OnItemStartActivityListener) {
            Log.d("Annv - Fragment", "activity " + activity.getLocalClassName());
            listenerStartAct = (OnItemStartActivityListener) activity;
        } else {
            throw new ClassCastException(activity.toString()
                    + " must implemenet MyListFragment.OnItemSelectedListener");
        }
    }

    // May also be triggered from the Activity
    public void updateDetail() {
        // create fake data
        //        String newTime = String.valueOf(System.currentTimeMillis());
        //        // Send data to Activity
        //        listenerStartAct.onRssItemSelected(newTime);
        if (getActivity() instanceof OnItemSelectedListener) {
            listener.onRssItemSelected("start start");
        } else {
            String newTime = String.valueOf(System.currentTimeMillis());
            listenerStartAct.onRssStartActivity(newTime);
        }

    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!