关山初度尘未洗,策马扬鞭再奋蹄!这篇文章主要讲述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);
}
使用此代码注册广播
推荐阅读
- android(没有得到新的传出呼叫广播)
- BroadcastReceiver在Android上没有按预期工作
- Android BroadcastReceiver数据库访问与房间
- BroadcastReceiver和AlarmManager Android
- 如何在android中阅读所有即将发布的通知
- 没有为android.media.tv.action.INITIALIZE_PROGRAMS发送BroadcastReceiver通知
- HTML5如何改变网络安全性()
- 保护NodJS应用程序免受在线威胁的10种工具
- 如何测试和修复Heartbleed SSL漏洞()