2

I'm currently using FragmentStatePagerAdapter and I would like it to have a "navigate up" feature (DisplayHomeAsUpEnabled). When I set the following code it shows up as a correct navigate back button. But nothing happens when I click on it, can you guys see anything that I', doing wrong in my code, I get no error while pressing the "navigate up" button and I get no compiler errors.

Here is the code, its in the "public boolean onOptionsItemSelected(MenuItem item) {" it gets interesting at the bottom.

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);
    }
    }
}
4

3 に答える 3

6

追加してみてください:setHasOptionsMenu(true); ShareholdingFragmentのonCreate()で。

于 2012-12-13T20:08:49.640 に答える
2

新しいツールバーと ActionbarDrawerToggle を使用している場合。clickHandler を直接割り当てることができます。このドロワー ツールバーを使用するアクティビティでは、ルートにある場合にドロワーを有効にするインターフェイスを実装しました。

    @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.
            }
        });
    }
于 2015-03-02T07:26:20.687 に答える
2

setHomeButtonEnabled(true)アクション バーを構成するときに、API レベル 14 以降を追加します。API レベル 11-13 では、ホーム ボタンが自動的に有効になります。

于 2012-12-13T20:44:12.073 に答える