androidandroidxnavigationcontrollermat-stepper

Android Material Stepper Showing Empty View After Navigating Back To The View


i am using Android Material Stepper Library which is working fine for the first time initialization in fragment but when i come back to the fragment it is showing empty view i have tried everything tried to release stepper layout remove all views and everything else i could but still same response

  private void initStepper2() {
        Toast.makeText(getActivity(), "initStepper", Toast.LENGTH_SHORT).show();

        mStepperLayout = mainView.findViewById(R.id.stepperLayout);
        mStepperLayout.setSaveEnabled(false);


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                stepArrayList.clear();
                stepArrayList.add(new FirstStepFragment());

                stepArrayList.add(new ThirdStepFragment());


                stepperAdapter = new StepperAdapter(getActivity().getSupportFragmentManager(), getActivity(), stepArrayList);
                mStepperLayout.setAdapter(stepperAdapter);

                mStepperLayout.requestLayout();
                mStepperLayout.refreshDrawableState();
                stepperAdapter.notifyDataSetChanged();
            }
        }, 300);
    }

this is where i am initializing stepper

 @Override
    public void onDetach() {
        Toast.makeText(getActivity(), "OnDetach", Toast.LENGTH_SHORT).show();
//        mStepperLayout.removeAllViews();
//        mStepperLayout.removeAllViewsInLayout();
//        mStepperLayout.refreshDrawableState();
//        mStepperLayout.requestLayout();
//        mStepperLayout.setAdapter(null);

        super.onDetach();
    }

    @Override
    public void onDestroyView() {
        Toast.makeText(getActivity(), "onDestroyView", Toast.LENGTH_SHORT).show();
//        mStepperLayout.removeAllViews();
//        mStepperLayout.removeAllViewsInLayout();
//        mStepperLayout.refreshDrawableState();
//        mStepperLayout.requestLayout();
//        mStepperLayout.setAdapter(null);
        super.onDestroyView();

    }

    @Override
    public void onPause() {
        Toast.makeText(getActivity(), "onPause", Toast.LENGTH_SHORT).show();
//        mStepperLayout.removeAllViews();
//        mStepperLayout.removeAllViewsInLayout();
//        mStepperLayout.refreshDrawableState();
//        mStepperLayout.requestLayout();
//        mStepperLayout.setAdapter(null);
        super.onPause();
    }
    

i have tried all of these but still not getting it

enter image description here

this is what i am getting for the first time when initializing fragment

enter image description here

and this is the second time when i re-navigate to the fragment

 mainView.findViewById(R.id.L1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navController = Navigation.findNavController(mainView);
                navController.navigate(R.id.action_newComplaintFragment_to_registerNCFragment);
            }
        });

this is how i am navigating to the fragment using android x navigation controller via navigation map


Solution

  • You're using the wrong FragmentManager here:

    stepperAdapter = new StepperAdapter(getActivity().getSupportFragmentManager(), getActivity(), stepArrayList);
    

    Whenever your fragment is fully container within another (such as your mStepperLayout being part of your NewComplainFragment), you need to use the childFragmentManager and not the Activity's FragmentManager:

    stepperAdapter = new StepperAdapter(getChildFragmentManager(), getActivity(), stepArrayList);