Java 片段不';t单击后自动更新
我的操作栏中有一个片段,如果用户单击片段按钮,页面会自动更新/刷新其中的任何内容。我面临的问题是,片段在第一次时不会自动更新,但在第二次按下时会更新和刷新 这就是流程: 选择片段1->不更新/刷新->返回并从操作栏中选择另一个片段2->再次选择片段1->更新片段1的UI 我在异步任务的onPostExecute中从MainActivity调用片段1 主要活动Java 片段不';t单击后自动更新,java,android,fragment,refresh,Java,Android,Fragment,Refresh,我的操作栏中有一个片段,如果用户单击片段按钮,页面会自动更新/刷新其中的任何内容。我面临的问题是,片段在第一次时不会自动更新,但在第二次按下时会更新和刷新 这就是流程: 选择片段1->不更新/刷新->返回并从操作栏中选择另一个片段2->再次选择片段1->更新片段1的UI 我在异步任务的onPostExecute中从MainActivity调用片段1 主要活动 private final FragmentOne f1 = new FragmentOne(); @Override
private final FragmentOne f1 = new FragmentOne();
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
f1.updateUI();
}
我的fragment类中有这段代码
// Updates the fragment-1 UI
public void updateUI() {
if (isAdded()) {
String currentDatabaseName = null;
hideAllTables();
hideWarningLabelsAndHeaders();
progressBarIndicator.setVisibility(View.VISIBLE);
if (null != getUserIdentifier()) {
currentDatabaseName = Utils.buildUserDataFromFli(getUserIdentifier());
}
if (null != currentDatabaseName) {
shopData = new ShopDao(ShopApplication.getLoggedInUser(), Utils.getDatabaseIndexMap());
transcationData = new TranscationDao(ShopApplication.getLoggedInUser(), Utils.getDatabaseIndexMap());
}
configureUserDependentFetchers(getUserIdentifier());
}
}
试试这个,也许对你有用;) 关于活动:
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
//to send broadcast for refresh
Intent intentRefresh = new Intent("com.yourapp.REFRESH_DATA");
LocalBroadcastManager.getInstance(YourActivity.this).sendBroadcast(intentRefresh);
}
在你的片段上:
private final BroadcastReceiver broadcastReceiverMap = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("com.yourapp.REFRESH_DATA")) {
//put here code for refresh
}
}
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
//register your LocalBroadcatManager
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.yourapp.REFRESH_DATA");
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
broadcastReceiverMap, intentFilter);
return view;
}
试试这个,也许对你有用;) 关于活动:
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
//to send broadcast for refresh
Intent intentRefresh = new Intent("com.yourapp.REFRESH_DATA");
LocalBroadcastManager.getInstance(YourActivity.this).sendBroadcast(intentRefresh);
}
在你的片段上:
private final BroadcastReceiver broadcastReceiverMap = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("com.yourapp.REFRESH_DATA")) {
//put here code for refresh
}
}
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
//register your LocalBroadcatManager
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.yourapp.REFRESH_DATA");
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
broadcastReceiverMap, intentFilter);
return view;
}
调用asyntask的位置?第一次调用时,您的
isAdded
布尔值是否更新?添加片段时,检查布尔值是否正确/***如果片段当前添加到其活动中,则返回true。*/最后一个公共布尔值isAdded(){return mActivity!=null&&mAdded;}我在活动的onResume()部分调用updateUI。这可能是@IshitaSinhawhere调用asyntask的原因吗?您的isAdded
boolean是否在第一次调用时更新?添加的片段时检查布尔值是否正确/***如果片段当前添加到其活动中,则返回true。*/最后一个公共布尔值isAdded(){return mActivity!=null&&mAdded;}我在活动的onResume()部分调用updateUI。这可能是“IshitaSinha”的原因吗