关于fragment的传值问题

来源:互联网 发布:螺纹铣刀怎么编程 编辑:程序博客网 时间:2024/06/06 11:00

以下是摘录自http://stackoverflow.com/questions/13700798/basic-communication-between-two-fragments上的评论内容。

貌似官方提倡使用接口来实现fragment之间的传值问题,本文将持续更新。
以下方法可以解决fragment之间的通信问题,但是还有其他的方法,我将会总结后持续更新,欢迎留言讨论交流。


Basic Communication between two fragments

up vote6down votefavorite
4

I have one activity - MainActivity. Within this Activity I have two fragments, both of which I created declaratively within the xml.

I am trying to pass the String of text input by the user into Fragment A to the text view in Fragment B. However this is proving to be very difficult. Does anyone know how I might achieve this?

I am aware that a fragment can get a reference to it's activity using getActivity(). So im guessing I would start there?

shareimprove this question
 

4 Answers

activeoldestvotes
up vote9down voteaccepted

Have a look at the Android deverlopers page:http://developer.android.com/training/basics/fragments/communicating.html#DefineInterface

Basically, you define an interface in your Fragment A, and let your Activity implement that Interface. Now you can call the interface method in your Fragment, and your Activity will receive the event. Now in your activity, you can call your second Fragment to update the textview with the received value

// You Activity implements your interfacepublic class YourActivity implements FragmentA.TextClicked{    @Override    public void sendText(String text){        // Get Fragment B        FraB frag = (FragB)            getSupportFragmentManager().findFragmentById(R.id.fragment_b);        frag.updateText(text);    }}// Fragment A defines an Interface, and calls the method when neededpublic class FragA extends Fragment{    TextClicked mCallback;    public interface TextClicked{        public void sendText(String text);    }    @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 = (TextClicked) activity;        } catch (ClassCastException e) {            throw new ClassCastException(activity.toString()                + " must implement TextClicked");        }    }    public void someMethod(){        mCallback.sendText("YOUR TEXT");    }}// Fragment B has a public method to do something with the textpublic class FragB extends Fragment{    public void updateText(String text){        // Here you have it    }}
shareimprove this answer
 
 
How about this case, fragment a sent some string to fragment b and fragment b send some result to fragment a, both fragment must be communicate within activity? –  StackOverflowError Sep 19 '14 at 15:06 
 
@Entreco when i use getactivity in fragment B i get a Null Pointer Exception. Even textview is null in FragB. Any suggestions? –  hemanth kumar Sep 22 '14 at 4:16 
 
@hemanthkumar in that case you probbably have not yet added Fragment B to the activity. You can do this by including it into your activity layout file, or by using the FragmentManager in the activity, and add it there. Good luck –  Entreco Sep 22 '14 at 7:48
 
m getting the value in FragB and the value is cleared in onCreateView please help –  HappyMan Oct 1 '14 at 11:00
0 0
原创粉丝点击