您可以在下一个片段中获得响应后从上一个片段中广播意图确保仅加载下一个片段/选项卡您还可以在 Activity 中设置数据,因此如果对数据片段所做的任何更改都可以合并它并且不要覆盖destroyItem这将
再次帮助重新创建 fagment 对象,您将获得新的数据...当
您必须从下一个片段到上一个片段进行更改!
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
Intent intent = new Intent("key_to_identify_the_broadcast");
Bundle bundle = new Bundle();
bundle.putString("edttext", json.toString());
intent.putExtra("bundle_key_for_intent", bundle);
context.sendBroadcast(intent);
}
然后您可以使用 BroadcastReceiver 类在片段中接收捆绑包
private final BroadcastReceiver mHandleMessageReceiver = new
BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle =
intent.getExtras().getBundle("bundle_key_for_intent");
if(bundle!=null){
String edttext = bundle.getString("edttext");
}
//you can call any of your methods for using this bundle for your use case
}
};
在你的片段的 onCreateView() 中,你需要先注册广播接收器,否则这个广播接收器将不会被触发
IntentFilter filter = new IntentFilter("key_to_identify_the_broadcast");
getActivity().getApplicationContext().
registerReceiver(mHandleMessageReceiver, filter);
最后你可以取消注册接收器以避免任何异常
@Override
public void onDestroy() {
try {
getActivity().getApplicationContext().
unregisterReceiver(mHandleMessageReceiver);
} catch (Exception e) {
Log.e("UnRegister Error", "> " + e.getMessage());
}
super.onDestroy();
}
您可以在所有片段中创建单独的广播接收器,并使用相同的广播将数据广播到所有片段。您还可以对不同的片段使用不同的密钥,然后对特定的片段使用特定的密钥进行广播。