0

I have a fragment activity, which has a fragment A. Fragment A does some important things in onViewCreated method:

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mission1 = view.findViewById(R.id.mission1);
    mission2 = view.findViewById(R.id.mission2);
    mission3a = view.findViewById(R.id.mission3a);
    mission3b = view.findViewById(R.id.mission3b);

    imageButtonList.add(mission1);
    imageButtonList.add(mission2);
    imageButtonList.add(mission3a);
    imageButtonList.add(mission3b);
    prepareButtons();
}

OK, now, this fragment A, has a button which creates a new fragment B, but I want to add a "back" button in the new Fragment B, so I added fragment A into backstack when launching Fragment B:

FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
CampaignMissionFragment fragment = CampaignMissionFragment.newInstance(auxMission);
ft.replace(R.id.fragmentContainer, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();

And in Fragment B, I added this to back button onClick():

getFragmentManager().popBackStack();

The problem is that when I press back on Fragment B, onViewCreated method of Fragment A is being called, so my imageButtonList array is getting a wrong amount of buttons inside because of the same buttons are being inserted again.

What would be the correct way to solve this issue? I thought that Fragment had similar behavior to Activities, where you can solve this issue putting the code that you don't want to execute two times in onCreate. But in this case, I can't do that because my views are not available in onCreate method of the fragment.

halfer
  • 19,824
  • 17
  • 99
  • 186
NullPointerException
  • 36,107
  • 79
  • 222
  • 382

4 Answers4

0

You may try to clear the imageButtonList in onDestroyView, Another solution would be to check if savedInstanceState is not null and then avoid adding the button to the list if this is the case Also, no View related operation in onCreate()

User One
  • 455
  • 3
  • 11
0

Update your fragment activity's onBackPressed as -

 @Override
    public void onBackPressed() {
        FragmentManager fm = getFragmentManager();
        if (fm.getBackStackEntryCount() > 0) {
            fm.popBackStack();
        } else {
            super.onBackPressed();
        }
    }
Naresh NK
  • 1,036
  • 1
  • 9
  • 16
0

Check the savedInstanceState object to determine if this is the first time the Fragment is being initialized and only save your fields if it's the first time through:

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mission1 = view.findViewById(R.id.mission1);
    mission2 = view.findViewById(R.id.mission2);
    mission3a = view.findViewById(R.id.mission3a);
    mission3b = view.findViewById(R.id.mission3b);

    if (savedInstanceState == null) {
        imageButtonList.add(mission1);
        imageButtonList.add(mission2);
        imageButtonList.add(mission3a);
        imageButtonList.add(mission3b);
        prepareButtons();
    }
}
dominicoder
  • 9,338
  • 1
  • 26
  • 32
-1

Try to Write Code in OneResume

 @Override
public void onResume() {
    mission1 = view.findViewById(R.id.mission1);
    mission2 = view.findViewById(R.id.mission2);
    mission3a = view.findViewById(R.id.mission3a);
    mission3b = view.findViewById(R.id.mission3b);

    imageButtonList.add(mission1);
    imageButtonList.add(mission2);
    imageButtonList.add(mission3a);
    imageButtonList.add(mission3b);
    prepareButtons();
}
Nimesh Patel
  • 1,394
  • 13
  • 26
  • This would result in `imageButtonList` getting 4 new buttons addded to it every single time you resumed the fragment (basically anytime you left to another app and came back). This would be worse than what the OP is seeing now. – dominicoder Nov 18 '19 at 00:49