调用片段B时,片段A不会暂停

时间:2014-12-22 08:33:58

标签: android android-activity android-fragments fragment

我有两个片段,让我们调用第一个片段Fragment A和第二个片段Fragment BFragment A包含一个带有2个子片段的ViewPagerFragment B是一个片段,它会以某种方式从用户那里获得一些数据点。 Fragment A个子片段中的一个包含RecyclerView,该Fragment B应使用FrameLayout的数据点更新它的cardview。两个片段都在notifiyDatasetChanged内被替换。

问题在于,Fragment A RecyclerView永远不会Fragment A is never called Fragment B RecyclerView。当我点击onStop后退按钮时,它不会使用新数据更新Fragment B

当我打开RecyclerView时,如何让片段A暂停或致电// ViewPager fragment fm = getSupportFragmentManager(); ft = fm.beginTransaction(); ft.replace(R.id.show_fragment, ViewPagerFragment.newInstance()); ft.commit(); ?更新fm = getSupportFragmentManager(); ft = fm.beginTransaction(); ft.replace(R.id.show_fragment, SessionFragment.newInstance()); ft.addToBackStack(null); ft.commit(); 的最佳方式是什么?

片段A的交易

 /*
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;
}

片段B的交易

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)

OnBackPressed

{{1}}

______________________ LOG INFO _____________________

{{1}}

1 个答案:

答案 0 :(得分: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替换A,请执行以下操作:

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();

在调用后退按钮时,执行此操作以使片段A返回

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移动时,您将不得不这样做 - &gt;甲