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

关山初度尘未洗,策马扬鞭再奋蹄!这篇文章主要讲述Android如何成功截击请求,如何将数据从Activity传递到Fragment?相关的知识,希望能为你提供帮助。
我想通过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 herebroadcasterReceiver = 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); }

我还记得什么?为了使这项工作。
答案首先,在包装的任何地方创建一个interface。例如 -
public interface SyncDataListener { void refreshDashboard(String myId, String username, String userId); }

然后在您的Activity中,创建一个全局声明和setter / resetter方法,如 -
private SyncDataListener syncDataListener; public void setSyncDataListener(SyncDataListener syncDataListener) { this.syncDataListener = syncDataListener; }public void resetSyncDataListener(){ syncDataListener = null; }

【Android如何成功截击请求,如何将数据从Activity传递到Fragment()】在你的片段中接下来实现上面的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); }

还要确保在Fragment被破坏时重置侦听器实例,如 -
@Override public void onDestroyView() { super.onDestroyView(); ((MainActivity) getActivity()).resetSyncDataListener(); }

现在,无论何时您需要将数据从Activity发送到Fragment,您都可以致电 -
if (syncDataListener != null) { syncDataListener.refreshDashboard(myId, username, userId); }

另一答案使用此代码,这是我身边的工作:
private BroadcastReceiver broadcasterReceiver; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {//..other code herebroadcasterReceiver = 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); }

使用此代码注册广播

    推荐阅读