关于fragment的传值问题

Posted Mr.Biandan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于fragment的传值问题相关的知识,希望对你有一定的参考价值。

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

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


Basic Communication between two fragments

up vote 6 down vote favorite 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?

 
share improve this question edited Dec 4 '12 at 10:48 David Hammen
18.8k 5 17 50
asked  Dec 4 '12 at 10:26 Javacadabra
1,075 2 23 69
  add a comment

4 Answers

active oldest votes
up vote 9 down vote accepted

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 interface
public 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 needed
public 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 text
public class FragB extends Fragment

    public void updateText(String text)
        // Here you have it
    
share improve this answer answered  Dec 4 '12 at 10:41 Entreco
4,394 2 21 51
 
 
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

以上是关于关于fragment的传值问题的主要内容,如果未能解决你的问题,请参考以下文章

关于STRUTS2的传值问题?

关于c# 页面跳转之间的传值总结(摘抄)

关于c# 页面跳转之间的传值总结(摘抄)

关于bootstrap 表单 带有多选下拉列表和kkpager翻页 的传值问题

MUI APP关于页面之间的传值,plusready和自定义事件

WebGL关于网页端与U3D互动的传值方法