I have a fragment with a bottom sheet inside and I want to hide it when back button is pressed, then with the second back go to previous fragment,I tried to use onBackPressed
but it doesn't work. setOnKeyListener()
it doesn't work too. Some help solution, wellcome.
this may help you !
@Override
public void onBackPressed() {
touchToBack++;
if (touchToBack== 1) {
if (bottomSheetBehavior!=null && bottomSheetBehavior.getState() !=
BottomSheetBehavior.STATE_HIDDEN) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
touchToBack= 0;
return;
}
}
if (touchToBack== 1 || touchToBack> 1) {
if (bottomSheetBehavior!=null && bottomSheetBehavior.getState() ==
BottomSheetBehavior.STATE_HIDDEN) {
touchToBack= 0;
super.onBackPressed();
}
}
}
This is my code
public class ProfileImageFragment extends Fragment {
View view;
private ImageView profileImg, editIconImg, ivBsDelete, ivBsGallery;
private BottomSheetBehavior bottomSheetBehavior;
private RelativeLayout bottomSheetLayout;
private static final int REQUEST_READ_MEDIA = 1;
private static final int RESULT_OK = -1;
public ProfileImageFragment() {
// Required empty public constructor
}
public static ProfileImageFragment newInstance() {return new ProfileImageFragment();}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_profile_image, container, false);
profileImg = (ImageView) view.findViewById(R.id.profileImage);
editIconImg = (ImageView) view.findViewById(R.id.editProfileImage); //edit option
bottomSheetLayout = view.findViewById(R.id.bottom_sheet2);
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetLayout);
bottomSheetBehavior.setHideable(true);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
//when longpress on image appears the bottomSheet
profileImg.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
return true;
}
});
//when only clik on image hide the bottomSheet
profileImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bottomSheetBehavior.setHideable(true);//Important to add
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
});
//when only clik on image appears the bottomSheet
editIconImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
});
//delete option of bottomsheet
ivBsDelete = (ImageView) view.findViewById(R.id.ivBsDelete);
ivBsDelete.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
((MainActivity) getActivity()).imgPath = "";
//Picasso.with(getContext()).invalidate(imagePath); //clear cache
profileImg.setImageResource(R.drawable.user);
((MainActivity)getActivity()).imgProfile.setImageResource(R.mipmap.ic_launcher_round);
bottomSheetBehavior.setHideable(true);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
});
//add image from gallery option of bottomsheet
ivBsGallery = (ImageView) view.findViewById(R.id.ivBsGallery);
ivBsGallery.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
int permissionCheck = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_MEDIA);
}else{
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, REQUEST_READ_MEDIA);
}
bottomSheetBehavior.setHideable(true);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//GETTING IMAGE FROM GALLERY
if (requestCode == REQUEST_READ_MEDIA && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = this.getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
((MainActivity) getActivity()).imgPath = picturePath;
cursor.close();
Call<Response.LoadPictureProfile> call2 = ((MainActivity) getActivity()).getDataService.loadPictureProfile(((MainActivity) getActivity()).user.token, ((MainActivity) getActivity()).user.loadPictureProfile(picturePath));
call2.enqueue(new Callback<Response.LoadPictureProfile>() {
@Override
public void onResponse(Call<Response.LoadPictureProfile> call, retrofit2.Response<Response.LoadPictureProfile> response) {
((MainActivity) getActivity()).responseLoadPictureProfile = response.body();
if(((MainActivity) getActivity()).responseLoadPictureProfile.status)
{
alert("Request", ((MainActivity) getActivity()).responseLoadPictureProfile.toString());
}else{
alert("Mensaje", ((MainActivity) getActivity()).user.errorMsg(((MainActivity) getActivity()).responseLoadPictureProfile.error_code));
}
}
@Override
public void onFailure(Call<Response.LoadPictureProfile> call, Throwable t) {
}
});
Picasso.with(getContext())
.load(new File(((MainActivity) getActivity()).imgPath))
.fit()
.centerCrop()
.into(profileImg);
if(!((MainActivity) getActivity()).imgPath.isEmpty()){
Picasso.with(getContext())
.load(new File(((MainActivity) getActivity()).imgPath))
.fit()
.centerCrop()
.into(((MainActivity)getActivity()).imgProfile);
}
}
}
protected void alert(String title,String message){
android.app.AlertDialog alertDialog = new android.app.AlertDialog.Builder(getContext()).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton(android.app.AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.