ActionBar的后退主页按钮无法使用片段

问题描述 投票:0回答:1

我试图使用操作栏的后退按钮导航回上一个片段,但它无法正常工作。

后退动作与android硬件后退按钮完美配合,但我也想实现actionBar主页按钮。这是代码:

   package com.example.eapple.tripdatacollection;
    import android.media.Image;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.NavUtils;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.util.Log;
    import android.util.Printer;
    import android.view.LayoutInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.inputmethod.EditorInfo;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.Spinner;
    import android.widget.TextView;


    /**
     * A simple {@link Fragment} subclass.
     */
    public class AddDataFragment extends Fragment {

        //Tag for debugging logs
        private static final String TAG = "AddDataFragment";

        private Toolbar toolbar;
        private Button btnGetGpsLoc;
        private TextView tvLong;
        private TextView tvLat;
        private EditText etLocName;
        private EditText etDescription;
        private EditText etImageTitleOnCam;
        private ImageView ivAddImgBtn;
        private Spinner spnClass;
        private RadioGroup rgAccess;
        private RadioButton rbCar;
        private RadioButton rbJeep;
        private RadioButton rbTrack;
        private Button btnSave;

        public AddDataFragment() {
            // Required empty public constructor
        }



        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
            if(id == android.R.id.home){
                Log.d(TAG, "onOptionItem Selected: called");
                getFragmentManager().popBackStack();
            }
            return true;
        }





        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            Log.d(TAG, "onCreateView: Called");
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_add_data, container, false);
            String head = getArguments().getString("head");

            //Initializing objects
            toolbar = view.findViewById(R.id.app_bar_new);
            btnGetGpsLoc = view.findViewById(R.id.btn_get_location);
            tvLong = view.findViewById(R.id.tv_long);
            tvLat = view.findViewById(R.id.tv_lat);
            etLocName = view.findViewById(R.id.et_name_of_attr);
            etDescription = view.findViewById(R.id.et_description);
            etImageTitleOnCam = view.findViewById(R.id.et_cam_img_title);
            ivAddImgBtn = view.findViewById(R.id.btn_add_imgs);
            spnClass = view.findViewById(R.id.spin_classification);
            rgAccess = view.findViewById(R.id.rg_access);
            rbCar = view.findViewById(R.id.rb_car);
            rbJeep = view.findViewById(R.id.rb_jeep);
            rbTrack = view.findViewById(R.id.rb_track);

            //Getting reference to actionbar and doing customization
            toolbar = view.findViewById(R.id.app_bar_new);
            ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
            ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
            actionBar.setTitle(head);
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
    }

在任何情况下都不会调用onOptionsItemSelection函数。

android android-fragments android-actionbar back
1个回答
0
投票

在活动中......

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                //do something here like

     int backStackEntryCount 
             =getSupportFragmentManager().getBackStackEntryCount();

     if (backStackEntryCount > 0) {

        getSupportFragmentManager().popBackStack();

    }

    return true;
  }
return false;
}

in onCreate of Fragment put ...

 this.setHasOptionsMenu(true); //setHasOptionMenu(true) was not working

片段内的onOptionsItemSelected回调就像......

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
         int id = item.getItemId();
           //do something with your id
        return super.onOptionsItemSelected(item);
    }
© www.soinside.com 2019 - 2024. All rights reserved.