Android onOptionsItemSelected无效

时间:2014-05-02 07:02:27

标签: android android-fragments back-button

我正在使用我的片段进行回归功能。我使用操作栏作为返回按钮,但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);
    }

这段代码在我的片段(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. 我错过了什么吗?

3 个答案:

答案 0 :(得分:1)

当我发现堆栈实际上没有任何东西可以弹出时(我想知道为什么......),我选择了另一种方法。

我不知道这是否是最佳解决方案,但似乎有效。

替换

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()以返回。

答案 1 :(得分:0)

如何尝试在Activity和Fragment(A)上使用SupportFragmentManager

目前片段(A)正在使用

FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();

在你的活动onOptionsItemSelected正在使用

this.getFragmentManager().popBackStack();

将其更改为:

getSupportFragmentManager().popBackStack();

答案 2 :(得分:0)

更改此行,因为您正在处理来自FragmentTransaction课程的Fragment而不是Activity本身。您需要先获得getParentFragment()

FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();

FragmentTransaction transaction = getActivity().getParentFragment().getFragmentManager().beginTransaction();
        transaction.replace(R.id.bottom_tab_content, browseFragment);