gpt4 book ai didi

Android onOptionsItemSelected 无法运行

转载 作者:行者123 更新时间:2023-11-29 01:42:51 28 4
gpt4 key购买 nike

我正在使用我的 fragment 处理返回功能。我使用操作栏作为返回按钮,但函数 onOptionsItemSelected 不起作用(甚至可能未调用该函数)

此代码在我的 FragmentActivity 中:

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
getActionBar().setHomeButtonEnabled(false);//disable the back button
this.getFragmentManager().popBackStack();

return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_tab);

FragmentTabHost tabHost = (FragmentTabHost)findViewById(R.id.bottom_tab_host);

tabHost.setup(this, getSupportFragmentManager(), R.id.bottom_tab_content);

//tabHost.addTab(tabHost.newTabSpec("explore").setIndicator("explore"), ListTabWidget.class, null);
tabHost.addTab(tabHost.newTabSpec("browse").setIndicator("browse"), SearchList.class, null);
}

这段代码在我的 Fragment(A) 中:

            public class SearchList extends Fragment implements SearchView.OnQueryTextListener{

FragmentActivity searchActivity;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
searchActivity = (FragmentActivity) this.getActivity();

View searchView = inflater.inflate(R.layout.search_list, container, false);

SearchView searchBar = (SearchView)searchView.findViewById(R.id.browse_search);

searchBar.setIconifiedByDefault(false); //Showing text field in search
searchBar.setSubmitButtonEnabled(true);
searchBar.setOnQueryTextListener(this);

return searchView;
}

@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
/*Intent intent = new Intent(Intent.ACTION_SEARCH, null, searchActivity, BrowseGrid.class);
intent.putExtra(SearchManager.QUERY, query);

startActivity(intent);*/
Bundle args = new Bundle();
args.putString("search_query", query);

BrowseGrid browseFragment = new BrowseGrid();
browseFragment.setArguments(args);

FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.bottom_tab_content, browseFragment);
transaction.addToBackStack(null);

transaction.commit();

return false;
}

@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}


}

和 fragment (B):

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

gridView = (GridView)view.findViewById(R.id.gridView);

getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);

return view;
}

目的很简单。按下后退按钮,Fragment 从 B 返回到 A。我错过了什么吗?

最佳答案

因为我发现堆栈实际上没有任何东西可以弹出(我想知道为什么......),我选择了一个替代方法来弹出。

我不知道这是否是最佳解决方案,但它似乎可行。

替换

getFragmentManager().popBackStack();

FragmentManager fm = getFragmentManager();
if (fm.getBackStackEntryCount()>0){
//Log.d("Bottom Tab", "popping backstack");
fm.popBackStack();
} else {
//Log.d("Bottom Tab", "nothing can pop");
super.onBackPressed();
}

即使堆栈什么都没有,它也会覆盖onBackPressed()返回。

关于Android onOptionsItemSelected 无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23422583/

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