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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionBar的后退主页按钮无法使用片段相关的知识,希望对你有一定的参考价值。

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

后退动作与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函数。

答案

在活动中......

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

以上是关于ActionBar的后退主页按钮无法使用片段的主要内容,如果未能解决你的问题,请参考以下文章

如何在带有更多片段的操作栏上添加后退按钮

覆盖片段中的后退按钮

应用ActionBar中的返回按钮

带有片段的 Android Up 按钮未显示完整片段

Android ActionBar 后退按钮默认填充

Android - 将 ActionBar 后退按钮切换为导航按钮