Android如何成功截击请求,如何将数据从Activity传递到Fragment?

时间:2018-03-05 08:25:32

标签: android broadcastreceiver

我想通过BroadcastReceiver将数据从活动传递到片段。但我无法获取片段中的数据,导致onReceive()没有启动。

在我的AppConfig中:

public static final String CREATE_POST = "created_post";

在活动A中实现所有这些:

 StringRequest request = new StringRequest(Request.Method.POST, AppConfig.MYURL, new Response.Listener<String>(){
            @Override
            public void onResponse(String response) {

    Intent intent = new Intent(AppConfig.CREATE_POST);
    intent.putExtra("myId",myId);
    intent.putExtra("userId",userId);
    intent.putExtra("username",username);

    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

在带有应该接收数据的片段中我已经实现了这个:

private BroadcastReceiver broadcasterReceiver;

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

//..other code here

    broadcasterReceiver = new BroadcastReceiver() {
       @Override
       public void onReceive(Context context, Intent intent) {
          if(intent.getAction().equals(AppConfig.CREATE_POST)){
             Log.d("Broadcast","get called")
             //HERE I get the intent here
          }
       }

 }


//I already register the boardcast in onResume() and onPause()
@Override
    public void onResume() {
        super.onResume();
        //register the broadcaster
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(broadcasterReceiver,
                new IntentFilter(AppConfig.CREATE_POST));
    }

  @Override
  public void onPause() {
    super.onPause();
   //unregister the broadcaster
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(broadcasterReceiver);
    }

我还记得什么?为了使这项工作。

2 个答案:

答案 0 :(得分:1)

首先,在包中的任何位置创建interface。例如 -

public interface SyncDataListener {
    void refreshDashboard(String myId, String username, String userId);
}

然后在活动中,创建一个全局声明和设置/重置器方法,如 -

private SyncDataListener syncDataListener;

public void setSyncDataListener(SyncDataListener syncDataListener) {
    this.syncDataListener = syncDataListener;
}

public void resetSyncDataListener(){
    syncDataListener = null;
}

接下来在您的片段中实施上述interface并覆盖方法,如 -

public class DashboardFragment extends Fragment implements SyncDataListener {  
    @Override
    public void refreshDashboard(String myId, String username, String userId) {
        //Your code that deals with the data received from activity
    }
}

同样在 Fragment onAttach(Context context)方法中调用在活动中创建的setter方法,如 -

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    ((MainActivity) getActivity()).setSyncDataListener(this);
}

还要确保在片段被销毁时重置侦听器实例,如 -

@Override
public void onDestroyView() {
    super.onDestroyView();
    ((MainActivity) getActivity()).resetSyncDataListener();
}

现在,无论何时需要将数据从Activity发送到Fragment,您都可以致电 -

if (syncDataListener != null) {
    syncDataListener.refreshDashboard(myId, username, userId);
}

答案 1 :(得分:0)

使用此代码,这在我身边起作用:

private BroadcastReceiver broadcasterReceiver;

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

//..other code here

    broadcasterReceiver = new BroadcastReceiver() {
       @Override
       public void onReceive(Context context, Intent intent) {
          if(intent.getAction().equals(AppConfig.CREATE_POST)){
             Log.d("Broadcast","get called")
             //HERE I get the intent here
          }
       }

 }


//I already register the boardcast in onResume() and onPause()
@Override
    public void onResume() {
        super.onResume();
        //register the broadcaster
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(broadcasterReceiver,
                new IntentFilter(AppConfig.CREATE_POST));
    }

  @Override
  public void onPause() {
    super.onPause();
   //unregister the broadcaster
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(broadcasterReceiver);
    }

使用此代码注册广播