我正在尝试使用操作栏的“后退”按钮导航回上一个片段,但是它不起作用。
后退动作与anroid硬件后退按钮完美配合,但我也想实现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函数。
答案 0 :(得分: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;
}
在Fragment的onCreate中...
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);
}