How to close the current fragment by using Button like the back button?
From Fragment A, to go to B, replace A with B and use addToBackstack()
before commit()
.
Now From Fragment B, to go to C, first use popBackStackImmediate()
, this will bring back A. Now replace A with C, just like the first transaction.
Close a fragment on button click which is inside that fragment
Just call:
getActivity().onBackPressed();
Toolbar back button does not close current Fragment
I believe what your looking for is the NavigationListener
for the Toolbar
Widget:
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
but if your using a Theme with an ActionBar
and setting a supportActionBar then do something :
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
onBackPressed();
break;
}
return true;
}
How can I close the fragment?
There's no such thing like close the fragment, but you can remove the fragment from the stack. To pop the fragment use the following inside button click listener
getActivity().getFragmentManager().beginTransaction().remove(this).commit();
How can I set onClick to close fragment in ImageView?
Remove getActivity()
from closefragment()
private void closefragment() {
getFragmentManager().popBackStack();
}
Tap back button twice to terminate application in Fragment
I solved the problem by writing it in override fun onAttach
Please check the code below
override fun onAttach(context: Context) {
super.onAttach(context)
mContext = context as MainActivity
callback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (doubleBackToExitPressedOnce) {
activity?.finish()
}
doubleBackToExitPressedOnce = true
Handler().postDelayed({ doubleBackToExitPressedOnce = false }, 2000)
showSnackBar(
context = mContext,
layout = binding.layoutMain,
type = Constants.SnackBarTypes.Warn,
message = mContext.getString(R.string.tap_twice_to_terminate)
)
}
}
requireActivity().onBackPressedDispatcher.addCallback(this, callback)
}
Related Topics
How to Set Up Android Studio to Work Completely Offline
How to Access /Storage/Emulated/0/
How to Access Data/Data Folder in Android Device
How to Format Credit Card Expiry Date in Mm/Yy When Entered by User
How to Unlock Android Phone Through Adb
How to Place an Imageview on Top of Another Imageview in Android
How to Remove Android Auto-Suggest Underlining in Edittext
How to Disable Behind View Click Event Framelayout
Remove Green Border Around Android Application
How to Set Value for Each Item Button Increase and Decrease Click and Set That Value in Text View
How to Resolve Javax.Xml.Bind.Jaxbcontext in Eclipse
Touchableopacity Not Working Inside an Absolute Positioned View
Retrofit 2.0 - How to Get Response Body for 400 Bad Request Error
Android Gradle Error When Sync Style Attribute Not Found
How to Get Device Id in Flutter of Both Android and Ios
Fragment Is Not Being Replaced But Put on Top of the Previous One
How to Retry Retrofit Call on Http Errors (401) When Using Rxjava