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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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);
    }

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

答案

首先,在包装的任何地方创建一个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;
}

在你的片段中接下来实现上面的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 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);
    }

使用此代码注册广播

以上是关于Android如何成功截击请求,如何将数据从Activity传递到Fragment?的主要内容,如果未能解决你的问题,请参考以下文章

如何在后台的两个 android 活动之间交换数据以通过蓝牙发送数据?

Android:如何从我的 APK 中将文件系统挂载到 RW 中? (当然是有根的)

如何从ajax成功函数返回数据?

如何在不刷新页面的情况下将数据从 Android 发送到 PHP 并显示

如何将消息从 Flutter 传递到 Native?

Android google Places API超出了每日配额