为什么我的android.R.id.home没有在我的片段中调用?

时间:2012-12-13 20:06:20

标签: java android android-layout

我目前正在使用FragmentStatePagerAdapter,我希望它具有“向上导航”功能(DisplayHomeAsUpEnabled)。当我设置以下代码时,它显示为正确的导航后退按钮。但是当我点击它时没有任何反应,你们可以看到我在我的代码中做错了什么,在按下“向上导航”按钮时没有错误,我没有编译错误。

这是代码,它在“public boolean onOptionsItemSelected(MenuItem item){”它在底部变得有趣。

public class ShareholdingDetailFragment extends FragmentActivity {

    final int NUM_ITEMS = Portfolio.getPortfolio().count();

    MyAdapter mAdapter;

    ViewPager mPager;
    Bundle extras; 


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        System.out.println(NUM_ITEMS + "e");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shareholdingdetailview_fragment_wrapper);

        mAdapter = new MyAdapter(getSupportFragmentManager());

        mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);


    }

    public class MyAdapter extends FragmentStatePagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        public int getCount() {
            return NUM_ITEMS;
        }

        public Fragment getItem(int position) {
            return ShareholdingFragment.newInstance(position);
        }
    }

    public static class ShareholdingFragment extends Fragment {
        int mNum;

        static ShareholdingFragment newInstance(int num) {
            ShareholdingFragment f = new ShareholdingFragment();

            // Supply num input as an argument.
            Bundle args = new Bundle();
            args.putInt("num", num);
            f.setArguments(args);

            return f;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mNum = getArguments() != null ? getArguments().getInt("num") : 1;
        }

        @SuppressLint({ "NewApi", "NewApi" })
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.shareholdingdetailview_fragment, container, false);

            /****************Setting the Display as home and it shows*******************/
            ActionBar bar = getActivity().getActionBar();
            bar.setDisplayHomeAsUpEnabled(true);
            /**********************************/

            return view;
        }

            @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        switch (item.getItemId()) {
                case android.R.id.home://Not working
System.out.println("test");//This isn't printed out
                    Intent upIntent = new Intent(getActivity(), DetailShareHoldingActivity.class);
                    if (NavUtils.shouldUpRecreateTask(getActivity(), upIntent)) {


                    getActivity().finish();
                } else {
                    NavUtils.navigateUpTo(getActivity(), upIntent);
                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    }
}

3 个答案:

答案 0 :(得分:5)

尝试添加:setHasOptionsMenu(true);在ShareholdingFragment中的onCreate()中。

答案 1 :(得分:2)

在配置操作栏时,在API级别14及更高级别添加setHomeButtonEnabled(true)。在API级别11-13,主页按钮自动启用。

答案 2 :(得分:2)

如果您正在使用新工具栏和ActionbarDrawerToggle。您可以直接分配clickHandler。对于我有这个抽屉工具栏的活动,我实现了一个接口,以便在root用户启用抽屉,

    @Override
    public void enableDrawer(boolean enable) {
        mDrawerToggle.setDrawerIndicatorEnabled(enable);
        mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
// Pop fragment back stack or pass in a custom click handler from the fragment.
            }
        });
    }