我有兩個片段,我們稱第一個片段爲Fragment A
,第二個片段爲Fragment B
。 Fragment 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)
您n在替換之前手動調用片段的onPause方法。 –
你能告訴我一些示例代碼嗎?最佳做法是什麼? – AndyRoid
是的,你可以做的是這樣做,拿起一個堆棧,並推動第一個片段A ..當你需要用B替換A時,那麼在這種情況下,替換將新的片段B推入堆棧,然後選擇最後一個元素和調用onPause –