2014-12-22 116 views
1

我有兩個片段,我們稱第一個片段爲Fragment A,第二個片段爲Fragment BFragment A包含ViewPager帶有2個子片段,Fragment B是一個單一的片段,它將以某種方式從用戶那裏獲取一些數據點。 Fragment A的子片段中的一個包含RecyclerView,它應該用來自Fragment B的數據點更新它的卡片視圖。這兩個片段在FrameLayout內被替換。當片段B被調用時,片段A不會暫停

問題是notifiyDatasetChanged從不要求Fragment A s RecyclerView因爲Fragment A is never called。當我從Fragment B中點擊後退按鈕時,它不會使用新數據更新RecyclerView

當我打開Fragment B時,如何讓片段A暫停或撥打onStop?更新RecyclerView的最佳方法是什麼?

片段A的事務

// ViewPager fragment 
    fm = getSupportFragmentManager(); 
    ft = fm.beginTransaction(); 
    ft.replace(R.id.show_fragment, ViewPagerFragment.newInstance()); 
    ft.commit(); 

片段B的事務

fm = getSupportFragmentManager(); 
      ft = fm.beginTransaction(); 
      ft.replace(R.id.show_fragment, SessionFragment.newInstance()); 
      ft.addToBackStack(null); 
      ft.commit(); 

OnBackPressed

/* 
When a fragment is open allow you to go back to the activity when back button is pressed 
*/ 
@Override 
public void onBackPressed() { 

    FragmentManager fragmentManager = getSupportFragmentManager(); 

    if (onBackPressed(fragmentManager)) { 
     return; 
    } else { 
     super.onBackPressed(); 
     button.setVisibility(View.VISIBLE); 
     button.setEnabled(true); 
    } 

    super.onBackPressed(); 
} 


private boolean onBackPressed(FragmentManager fm) { 
    if (fm != null) { 
     if (fm.getBackStackEntryCount() > 0) { 
      fm.popBackStack(); 
      button.setVisibility(View.VISIBLE); 
      button.setEnabled(true); 
      return true; 
     } 

     List<Fragment> fragList = fm.getFragments(); 
     if (fragList != null && fragList.size() > 0) { 
      for (Fragment frag : fragList) { 
       if (frag == null) { 
        continue; 
       } 
       if (frag.isVisible()) { 
        if (onBackPressed(frag.getChildFragmentManager())) { 
         return true; 
        } 
       } 
      } 
     } 
    } 

    return false; 
} 

______________________日誌信息_____________________

12-22 05:38:00.268 18255-18255/? E/InputEventSender﹕ Exception dispatching finished 

signal. 
12-22 05:38:00.268 18255-18255/? E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback 
12-22 05:38:00.278 18255-18255/? E/MessageQueue-JNI﹕ java.util.NoSuchElementException 
      at java.util.Vector.lastElement(Vector.java:621) 
      at com.project.ui.MainActivity.onBackPressed(MainActivity.java:267) 
      at com.project.ui.MainActivity.onBackPressed(MainActivity.java:251) 
      at android.app.Activity.onKeyUp(Activity.java:2453) 
      at android.view.KeyEvent.dispatch(KeyEvent.java:2633) 
      at android.app.Activity.dispatchKeyEvent(Activity.java:2704) 
      at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2221) 
      at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3918) 
      at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3880) 
      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449) 
      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502) 
      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468) 
      at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578) 
      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476) 
      at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635) 
      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449) 
      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502) 
      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468) 
      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476) 
      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449) 
      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502) 
      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468) 
      at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3611) 
      at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3772) 
      at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2208) 
      at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1849) 
      at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1840) 
      at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2185) 
      at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141) 
      at android.os.MessageQueue.nativePollOnce(Native Method) 
      at android.os.MessageQueue.next(MessageQueue.java:143) 
      at android.os.Looper.loop(Looper.java:122) 
      at android.app.ActivityThread.main(ActivityThread.java:5221) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
12-22 05:38:00.278 18255-18255/? D/AndroidRuntime﹕ Shutting down VM 
12-22 05:38:00.280 18255-18255/? E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: example.com.project, PID: 18255 
    java.util.NoSuchElementException 
      at java.util.Vector.lastElement(Vector.java:621) 
      at com.project.ui.MainActivity.onBackPressed(MainActivity.java:267) 
      at com.project.ui.MainActivity.onBackPressed(MainActivity.java:251) 
      at android.app.Activity.onKeyUp(Activity.java:2453) 
      at android.view.KeyEvent.dispatch(KeyEvent.java:2633) 
      at android.app.Activity.dispatchKeyEvent(Activity.java:2704) 
      at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2221) 
      at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3918) 
      at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3880) 
      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449) 
      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502) 
      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468) 
      at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578) 
      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476) 
      at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635) 
      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449) 
      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502) 
      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468) 
      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476) 
      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449) 
      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502) 
      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468) 
      at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3611) 
      at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3772) 
      at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2208) 
      at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1849) 
      at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1840) 
      at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2185) 
      at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141) 
      at android.os.MessageQueue.nativePollOnce(Native Method) 
      at android.os.MessageQueue.next(MessageQueue.java:143) 
      at android.os.Looper.loop(Looper.java:122) 
      at android.app.ActivityThread.main(ActivityThread.java:5221) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
+1

您n在替換之前手動調用片段的onPause方法。 –

+0

你能告訴我一些示例代碼嗎?最佳做法是什麼? – AndyRoid

+0

是的,你可以做的是這樣做,拿起一個堆棧,並推動第一個片段A ..當你需要用B替換A時,那麼在這種情況下,替換將新的片段B推入堆棧,然後選擇最後一個元素和調用onPause –

回答

0

聲明一個堆棧對象

Stack<Fragment> fragmentStack = new Stack<Fragment>(); 

當碎片A被替換第一次做到這一點:

fm = getSupportFragmentManager(); 
ft = fm.beginTransaction(); 
mViewPagerFragment = ViewPagerFragment.newInstance(); 
ft.replace(R.id.show_fragment, mViewPagerFragment); 
fragmentStack.push(mViewPagerFragment); 
ft.commit(); 

當您必須更換與B,這樣做:

fm = getSupportFragmentManager(); 
ft = fm.beginTransaction(); 
fragmnetStack.lastElement().onPause(); 
mSessionFragment = SessionFragment.newInstance(); 
ft.replace(R.id.show_fragment, mSessionFragment); 
fragmentStack.push(mSessionFragment); 
ft.addToBackStack(null); 
ft.commit(); 

在調用的返回按鈕,這樣做是爲了獲得片段背

fragmentStack.lastElement().onPause(); 
fragmentStack.pop(); 
fm = getSupportFragmentManager(); 
ft = fm.beginTransaction(); 
mViewPagerFragment = fragmentStack.lastElement(); 
mViewPagerFragment.onResume(); 
ft.replace(R.id.show_fragment, mViewPagerFragment); 
ft.commit(); 

編輯:

之前添加片段B,刪除片段A這樣的:

fm = getSupportFragmentManager(); 
ft = fm.beginTransaction(); 
ft.remove(fragmentStack.pop()); 
// then add the other code to add fragment B 

當您嘗試從B移動時您必須執行相同操作 - > A

+0

已檢查,但沒有工作錯誤是NoSuchElementException – AndyRoid

+0

我想ul必須先刪除先前的片段,等待檢查編輯 –

+0

也許這是我的錯誤,不足以解釋清楚。我的viewpager的第二個子片段中有一個recyclerview,它位於片段A中。在包含片段A和B的活動中,片段A最初被啓動,然後有一個按鈕來給片段B和.replace充氣,我的問題是在片段B中獲得的值不能在片段A的子回收器視圖中正確存儲。我檢查了日誌語句和onResume不被稱爲一些奇怪的原因?? – AndyRoid

相關問題