BroadcastReceiver为了将数据从另一个Activity传递到Fragment [英] BroadcastReceiver in order to pass data from another Activity to Fragment

查看:203
本文介绍了BroadcastReceiver为了将数据从另一个Activity传递到Fragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有MainActivity,其中ViewPager中的片段很少.我想将数据从另一个活动传递到这些片段之一.我正在通过BroadcastReceiver进行此操作.

Let's say I have MainActivity where are few Fragments in ViewPager. I want to pass data from another Activity to one of these fragments. I'm doing this by BroadcastReceiver.

public class MyFragment extends Fragment {

    private MyFragmentReceiver mReceiver;

    public MyFragment() {
        super();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mReceiver = new MyFragmentReceiver();
        getActivity().registerReceiver(mReceiver, new IntentFilter("fragmentUpdater"));
    }

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

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // My code here
    }

    public class MyFragmentReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            //My methods
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mReceiver != null)
            getActivity().unregisterReceiver(mReceiver);
    }
}

因此,在我的AnotherActivity中,我正在执行以下操作:

So in my AnotherActivity I'm doing something like this:

Intent data = new Intent("fragmentUpdater");
MyApplication.getInstance().getMainActivity().sendBroadcast(data);

其中MyApplication是单例,其中包含MainActivity. 我注意到BroadcastReceiver正在将某些内容写入日志,我想知道这是执行此操作的最佳方法.

Where MyApplication is singleton which contains MainActivity. I noticed that BroadcastReceiver is putting something into logs, and I am wondering is that the best way to do it.

  1. 是否有更好的方法将数据从另一个活动传递到特定的Fragment或调用该Fragment中的方法?
  2. 我是否必须在AndroidManifest.xml中添加与BroadcastReceiver相关的内容?

推荐答案

一种替代方法是使用接口在活动和片段之间进行通信.示例:

One alternative is using an interface for communicating between your activity and fragments. Example:

界面

public interface MyInterface {

    void setSomeValue(int someValue);
    int getSomeValue();

}

活动

public class MyActivity extends Activity implements MyInterface {

    private int someValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // do the usual stuff
    }

    // implement from MyInterface
    @Override
    public void setSomeValue(int someValue) {
        this.someValue = someValue;
    }

    // implement from MyInterface
    @Override
    public int getSomeValue() {
        return someValue;
    }

}

片段

public class MyFragment extends Fragment {

    private MyInterface mi;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mi = (MyInterface) context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) {

        mi.setSomeValue(20);
        int someValue = mi.getSomeValue();

    }

}

您可以使用该界面在一个或多个活动,多个片段,视图,任务,服务等之间进行通信.如果要走这条路线,我将创建一个实现MyInterface及其方法的基础活动,并让所有其他活动扩展基本活动.我什至会创建一个调用onAttach()的基础片段,并让我的所有其他片段都扩展该基础片段(这样就不必在每个片段中都调用onAttach()了.)

You can use the interface to communicate between one or more activities, multiple fragments, views, tasks, services, etc etc etc. If you were to go this route, I would create a base activity which implements MyInterface and its methods, and have all other activities extend the base activity. I would even create a base fragment which calls onAttach(), and have all my other fragments extend this base fragment (so that I don't need to call onAttach() in every fragment).

更新...

基本片段看起来像这样:

A base fragment would simply look like this:

public class BaseFragment extends Fragment {

    public MyInterface mi;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mi = (MyInterface) context;
    }

}

现在,MyFragment会扩展BaseFragment ...

Now, MyFragment would just extend BaseFragment...

public class MyFragment extends BaseFragment {
    ...
}

现在不需要在扩展BaseFragment的任何片段中附加甚至声明MyInterface,该基本片段已经具有它的公共实例.您只需通过界面设置/获取/等即可,而无需大惊小怪:

There's no need now to attach or even declare MyInterface in any fragment extending BaseFragment, the base fragment already has a public instance of it. You just set/get/etc via your interface without any additional fuss:

mi.setSomeValue(20);

这篇关于BroadcastReceiver为了将数据从另一个Activity传递到Fragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆