首页 > 解决方案 > 通过后退按钮切换到片段时,OnClickListener 停止工作

问题描述

我的代码打开了一个按钮列表,这些按钮指向一个包含有关我的展位的更多详细信息的片段。如果我从主菜单打开列表,一切正常,但如果我在细节片段中按下后退按钮,则 OnClickListener 都不起作用,我无法进入细节片段。如果我回到主菜单,它会再次起作用。

抱歉代码乱七八糟!

public class BoothFragment extends AbstractArgoFragment {

// dependencies
@Inject
AppPreferenceAccessor appPreferenceAccessor;


private ArrayList<Button> buttonList = new ArrayList<>();

private HashMap<Button, Booth> listMap = new HashMap<>();

private BoothList b = new BoothList();
private ArrayList<Booth> boothList = b.getBooths();

public BoothFragment() {
    super(FragmentType.BOOTH);
}


@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(false);
}

@Override
protected void injectFrom(ArgoComponent injector){
    injector.inject(this);
}

@Nullable
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_booths, container, false);
    Button button1 = (Button) view.findViewById(R.id.booth1);      
    Button button2 = (Button) view.findViewById(R.id.booth2);
    Button button3 = (Button) view.findViewById(R.id.booth3);
    Button button4 = (Button) view.findViewById(R.id.booth4);
    Button button5 = (Button) view.findViewById(R.id.booth5);
    Button button6 = (Button) view.findViewById(R.id.booth6);
    Button button7 = (Button) view.findViewById(R.id.booth7);
    Button button8 = (Button) view.findViewById(R.id.booth8);
    Button button9 = (Button) view.findViewById(R.id.booth9);
    Button button10 = (Button) view.findViewById(R.id.booth10);
    Button button11 = (Button) view.findViewById(R.id.booth11);
    Button button12 = (Button) view.findViewById(R.id.booth12);
    Button button13 = (Button) view.findViewById(R.id.booth13);
    Button button14 = (Button) view.findViewById(R.id.booth14);
    Button button15 = (Button) view.findViewById(R.id.booth15);
    Button button16 = (Button) view.findViewById(R.id.booth16);
    Button button17 = (Button) view.findViewById(R.id.booth17);
    Button button18 = (Button) view.findViewById(R.id.booth18);
    Button button19 = (Button) view.findViewById(R.id.booth19);

    buttonList.add(button1);
    buttonList.add(button2);
    buttonList.add(button3);
    buttonList.add(button4);
    buttonList.add(button5);
    buttonList.add(button6);
    buttonList.add(button7);
    buttonList.add(button8);
    buttonList.add(button9);
    buttonList.add(button10);
    buttonList.add(button11);
    buttonList.add(button12);
    buttonList.add(button13);
    buttonList.add(button14);
    buttonList.add(button15);
    buttonList.add(button16);
    buttonList.add(button17);
    buttonList.add(button18);
    buttonList.add(button19);

    for (int i = 0; i < 19; i++) {
        listMap.put(buttonList.get(i), boothList.get(i));
        buttonList.get(i).setOnClickListener(this::onClick);
    }

    return view;
}

    public void onClick(View view) {
        Button button = (Button) view.findViewById(view.getId());

        Bundle bundle = new Bundle();
        bundle.putSerializable("booth",listMap.get(button));


        Toast.makeText(getActivity(), "Click B1", Toast.LENGTH_SHORT).show();
        getMainActivity().showFragment(FragmentType.BOOTH_DETAIL, bundle);
    }

  @Override
   public void onResume() {
      super.onResume();
      appPreferenceAccessor.setInstructionsRead();
   }
}

标签: javaandroid

解决方案


推荐阅读