关于fragment的传值问题

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

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


Basic Communication between two fragments

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
 

4 Answers

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
 
 
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值