2016-10-17 89 views
0

我在我的操作欄中有一個片段,如果用戶單擊片段按鈕,頁面應該自動更新/刷新,如果其中有任何內容。我面臨的問題是,片段不會自動更新第一次,而是在第二次按下時更新和刷新。片段不會自動更新點擊

這是流量:

選擇片段1 - >不更新/刷新 - >返回並從操作欄中選擇其他片段2 - >再次選擇片段1 - >更新片段1的UI 。

我在異步任務的onPostExecute中從MainActivity調用片段1。

MainActivity,

private final FragmentOne f1 = new FragmentOne(); 

@Override 
     protected void onPostExecute(Object o) { 
      super.onPostExecute(o); 
      f1.updateUI(); 
     } 

我有這樣一段代碼在我的片段類。

// 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()); 
    } 
} 
+0

,你叫asyntask? – sasikumar

+0

第一次調用是否更新了'isAdded'布爾值? –

+0

添加片段時檢查布爾值是否正確 – sasikumar

回答

2

試試這個,你也許工作;)

在活動時間:

@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; 
}