java - 通过后退按钮切换到片段时,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();
}
}
解决方案
推荐阅读
- php - 订单退款时如何访问woocommerce订单退款对象
- java - 在具有相同 id 的中央多对多关系表中的休眠中插入问题(它可以手动执行但不能自动执行)
- office365 - Viva 连接和 Microsoft 团队应用程序有什么区别
- perl - 当我打印哈希值时,不断得到类似 2/8HASH(0x73609f8) 的东西
- android - Native Checkout Paypal Button 未在 android 中为发布 apk 呈现
- java - Appium Desktop 无法及时找到连接的物理设备
- python - 如何在 Python 中使用子进程在文件名中添加变量
- java - 有没有一种方法可以使用 android-sqlite 游标从列数未知的表中选择 *
- python - 如何在熊猫中融化数据框并连接字符串以获得值
- python - 如何迭代数据框列表?