我正在使用片段处理返回功能。我使用操作栏作为返回按钮,但是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;
    }


}


和片段(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;
    }


目的很简单。按下返回按钮,片段从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 - Android onOptionsItemSelected无法正常运行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23422583/

10-11 16:25