Using DrawerLayout with Fragments and getting up n

2019-07-21 10:43发布

问题:

I am using a DrawerLayout as the way to navigate the various screens of my app. I have a few different items in the drawer such as:

  • News
  • Photos
  • Events
  • Menu

Each one of those is a Fragment (mostly ListFragment), and then you can navigate deeper by tapping a row in the list to take you to another Fragment. E.g., seeing a list of photo galleries, then tapping one of them and it will show the photos.

I am able to tap the drawer icon or slide from the left to show the drawer and choose any of the options and it will correctly show that Fragment. But, when I go into a deeper fragment (going to photos then tapping a gallery to see the list of photos) the drawer icon does not change to a back icon and allow the user to navigate back to the previous Fragment. I can tap the hardware back button and it will go back, but not with the button.

In my MainActivity I have:

getActionBar().setDisplayHomeAsUpEnabled(true);

In Photos, when tapping a row that represents a gallery I have:

list.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        PUCObjects.PUCPhotoGalleryItem item = items.get(position);

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        //ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);

        PUCPhotosGridFragment newFragment = new PUCPhotosGridFragment();
        newFragment.galleryId = item.assetId;

        ft.add(R.id.container_frame, newFragment, ""+position);
        ft.addToBackStack(null);

        // Start the animated transition.
        ft.commit();

    }//end

});

And then when it loads list the list of photos Fragment, in `onActivityCreated():

// Show loading view
MainActivity main = (MainActivity)getActivity();
main.showLoadingActivity(true);

// Show the back button
main.getActionBar().setDisplayHomeAsUpEnabled(true);

How can I get up navigation working and show the back button when I am in deeper Fragments? What have I missed that will allow me to navigate back up the stack?