碎片交易重置活动

时间:2018-08-06 05:07:57

标签: java android android-fragments android-activity

我有一个活动,该活动根据用户操作(单击按钮)或事件(触发LocalBroadcast的FCM数据消息)加载多个片段。

我最近在将Fragment Transaction放入BroadcastReceiver中时遇到了一个障碍,并且一旦触发接收器,而不是加载下一个片段,我得到了第一个(默认)片段,该片段被加载到OnCreate中活动,表示该活动已以某种方式重置/重新启动。

鉴于此速度,我在重置Android Studio上的logcat之前设法看到的唯一错误是:

  

java.lang.IllegalStateException:之后无法执行此操作   onSaveInstanceState

except for this article on Activity State Loss是我发现的,四处搜寻并没有太大帮助。它虽然比较老(2013年),但似乎很有意义。但是,我没有想到的解决方案,除了使我的片段变得更加复杂,并将下一个片段的逻辑交给这个片段本身之外。

请在下面找到发生这种情况的代码段。

BroadcastReceiver assistanceReceivedStatusReceiver = new BroadcastReceiver() {

        public void dummyfunc(){
            return;
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            // this is triggered by the localBroadcast from FCM Service
            boolean requestresult = intent.getBooleanExtra("success", true);
            if(!requestresult) {
                // we don't have a responder
                Log.d(TAG, "onReceive: =======================================");
                Log.d(TAG, "onReceive: =======================================");
                Log.d(TAG, "onReceive: UNABLE TO FIND A RESPONDER");
                Log.d(TAG, "onReceive: =======================================");
                Log.d(TAG, "onReceive: =======================================");
                String message = "Unable to find you a responder, please try again!";
                frameAnimation.stop();
                txtRequestStatus.setText(message);
                dialogButtonLayout.setVisibility(View.VISIBLE);
                showBottomAppBar();
                showMenuFab();
                moveMenuRight();
                setMenuImage(R.drawable.baseline_undo_white_24dp);
                menuButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialogLayout.setVisibility(View.GONE);
                        waitingLayout.setVisibility(View.VISIBLE);
                        moveMenuCenter();
                        resetMenuImage();
                        menuButton.setOnClickListener(defaultMenuButtonListener);
                    }
                });

            } else {
                // we have a responder
                // this is a one time receiver - set up an Observable for the Live<Incident>
                // and unregister self.
                Log.d(TAG, "onReceive: =======================================");
                Log.d(TAG, "onReceive: =======================================");
                Log.d(TAG, "onReceive: RECEIVED A RESPONDER");
                Log.d(TAG, "onReceive: =======================================");
                Log.d(TAG, "onReceive: =======================================");
                inIncident = true;
                Bundle nextbundle = new Bundle();
                responderinfo = intent.getBundleExtra("responderinfo");
                nextbundle.putParcelable("data", intent.getBundleExtra("data"));
                nextbundle.putBundle("responderinfo", responderinfo);
                // GO! GO! GO!!!
                //startFragmentWithArgs(new RequestAssistFragmentDeliver(),nextbundle );
                RequestAssistFragmentDeliver deliver = new RequestAssistFragmentDeliver();
                deliver.setArguments(nextbundle);
                ((Reviv) getActivity()).getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.containerFrameLayout, deliver).commitAllowingStateLoss();
            }
        }
    };

有什么问题的想法吗?与往常一样,我很乐意根据需要共享更多信息(代码库庞大,知道需要什么可以帮助我共享相关部分)。

更新1: 根据Udit的要求共享功能。这些是包装函数,有助于使代码更具可读性。 bottomAppBar(BottomAppBar)和menuButton(FAB)是在Activity中加载的视图,我通过调用Activity中定义的getter在每个Fragment中建立关联。

(MainActivity)getActivity.getBottomAppBar();

功能:

private void showBottomAppBar(){
        bottomAppBar.setVisibility(View.VISIBLE);
        menuButton.setVisibility(View.VISIBLE);
    }

    private void moveMenuRight(){
        bottomAppBar.setFabAlignmentMode(BottomAppBar.FAB_ALIGNMENT_MODE_END);
    }

    private void showMenuFab(){
        //bottomAppBar.setFabAttached(true);
        menuButton.setVisibility(View.VISIBLE);
    }

1 个答案:

答案 0 :(得分:0)

据我所知,由于添加片段(java.lang.IllegalStateException)时发生崩溃,您的活动正在重新开始。

要确认,您可以替换

.commit() 

方法

.commitAllowingStateLoss()

看看这是否能解决您的问题