gpt4 book ai didi

android - 当在 actionBar 上按下后退按钮时,不会调用 onOptionsItemSelected

转载 作者:行者123 更新时间:2023-11-30 01:31:59 27 4
gpt4 key购买 nike

就这么简单,当我按下后退按钮时没有任何反应。这是我正在运行的代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tm_activity_inicio_resumen, container, false);

((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setHomeAsUpIndicator(R.drawable.back_left2);

return view;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getActivity().getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}

这个实现有问题吗?

最佳答案

更改此代码。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}

return super.onOptionsItemSelected(item);
}

关于android - 当在 actionBar 上按下后退按钮时,不会调用 onOptionsItemSelected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35606968/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com