1,初始化fragment时候:
@Override
public void onAttach(Activity activity) {
/** 注册广播 */
initBroadcast();
super.onAttach(activity);
}
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String dat = intent.getExtras().getString("data");
if (dat.equals("log_ok")) {
setData(); //获取数据并更新数据
} else {
double a = userinfo.getAvailableMoney()
- Double.parseDouble(dat);
userinfo.setAvailableMoney(a);// 改变可用额
st[4] = String.valueOf(a);
UserUtil.getInstant().saveUserArray(getActivity(), st);
// usable_cash_txt.setText(String.valueOf(a));
}
}
};
private void initBroadcast() {
// TODO Auto-generated method stub
IntentFilter filter = new IntentFilter(action);
getActivity().registerReceiver(broadcastReceiver, filter);
}
同时开启一个activity:
startActivity(new Intent(getActivity(), LoginActivityCenter.class));
2,在activity中获取网络数据后,进行数据存储,并广播给fragment;
Intent intent = new Intent(CenterFragment.action);
intent.putExtra("data", "log_ok");
sendBroadcast(intent);
LoginActivityCenter.this.finish();
3.在fragment重写方法onResume,获取activity存储的数据,并更新;
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
initBroadcast();
Log.i("i", " MyFragment onResume() ");
}