score:22
You have to override onBackPressed of parent FragmentActivity class. Therefore, put your codes in parent FragmentActivity. Or you can call parent's method by using this:
public void callParentMethod(){
getActivity().onBackPressed();
}
in FragmentActivity override onBackPressed Method and not call its super class to disable back button.
@Override
public void onBackPressed() {
//super.onBackPressed();
//create a dialog to ask yes no question whether or not the user wants to exit
...
}
score:-1
Just put a transparent view and set onClickListener for it
score:0
Just override onBackPressed
in your fragment where you want to disable the back press.
More info here: https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher
public class Login extends Fragment {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
OnBackPressedCallback callback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
// Log.d("TAG", "Pressed...");
}
};
requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
}
}
score:2
if you want to fragment never closed without any button action you can try to prevent cancel Fragment.
In your fragment method add this
this.setCancelable(false);
score:4
Change
public void onBackPressed() {
}
to
@Override
public void onBackPressed() {
super.onBackPressed()
}
OR
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
}
return super.onKeyDown(keyCode, event);
}
score:4
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onBackPressed() {
return;
}
score:9
I know it's too late, In fragment onCreate
val callback = requireActivity().onBackPressedDispatcher.addCallback(this) {
Log.d("tag","back button pressed") // Handle the back button event
}
callback.isEnabled
score:12
In your parent Activity
@Override
public void onBackPressed() {
Fragment f = getSupportFragmentManager().findFragmentById(R.id.content_frame);
if (f instanceof yourfragment) {//the fragment on which you want to handle your back press
Log.i("BACK PRESSED", "BACK PRESSED");
}else{
super.onBackPressed();
}
}
score:12
In your oncreateView() method you need to write this code and in KEYCODE_BACk return should true then it will stop the back button option for particular fragment
View v = inflater.inflate(R.layout.xyz, container, false);
//Back pressed Logic for fragment
v.setFocusableInTouchMode(true);
v.requestFocus();
v.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
}
return false;
}
});
score:12
OnBackPressedCallback
Here is the code which you can write in your Fragment class to customize the back button press.
public class MyFragment extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
OnBackPressedCallback callback = new OnBackPressedCallback(true /* enabled by default */) {
@Override
public void handleOnBackPressed() {
// Handle the back button even
Log.d("BACKBUTTON", "Back button clicks");
}
};
requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
}
}
Relevant link
You can read and research more on this HERE
score:19
Here is the new way you can manage your onBackPressed()
in fragment with the new call back of activity:
// Disable onBack click
requireActivity().onBackPressedDispatcher.addCallback(this) {
// With blank your fragment BackPressed will be disabled.
}
Here is the android doc link: https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher
Source: stackoverflow.com
Related Query
- How to disable back button pressed in android fragment class
- restart fragment class when back button press
- How to enable the button when text is entered and disable in Android java?
- How to make it work the back button in android webview?
- How to add back button to Android Web App
- How do I disable the Home button & Back button?
- How to attach back the Android emulator to ADB?
- How to get the graphical layout Window back in Eclipse for Android Application Design?
- How to disable android studio unit tests (androidTest)
- How to refresh fragment tab content on button click
- Eclipse creates fragment layout automatically, how can i disable it
- "Back" button on Action bar - Android . How to go "back"?
- Can you disable the back button in a JFace wizard?
- How to set the class path in Eclipse and Android Studio
- How to disable audio output processing on an htc phone with Android
- Override android hardware back button functionality
- How to Override Android Api Class with a class available in added jar?
- Why won't the Back button work in this Android webview?
- How to disable titlebar in eclipse android plugin Graphical layout preview?
- How to generate a class diagram from android project using papyrus
- Android Studio: How to import java class from outside the project?
- AVD-Ok button is disable while creating new android virtual device
- How to disable error like Eclipse while creating signed apk in Android Studio 1
- Android android.view.InflateException Binary XML file line #16: Error inflating class fragment
- how can i debugg an android service - activity that extends service class -
- how to disable Eclipse content-assist semicolon insertion (with Android project) - unchecking doesn't work
- Android lollipop Menu button to Back button rendering
- How do you disable Android Library Update?
- How to change Back and Next Button position in a JFace Wizard Dialog?
- How can I replace the implementation of an object class used by an Android Activity for my automated tests?
More Query from same tag
- GWT compilation error in Eclipse
- Where should i store my texture files in Eclipse Project
- Is there a complete eclipse package?
- Eclipse C++ Indexing Boost Takes Forever
- Activating the Spartan Refactoring plugin on a Java project
- NoSuchMethodError using commonc codec in Android application
- Android maven missing artifact errors
- Android SDK Java: not found
- How to package a jar file along with reference files?
- Eclipse debugger stops by itself without any throwable
- multiple classes in a single physical file on android
- proguard not working in Eclipse mac: Unable to access jarfile /Volumes/New
- ImageButtons Size and Placement not working in Eclipse
- Installing custom plugin on a clean Eclipse
- creating html pages in eclipse
- Eclipse EGit TFS Git Connection - Authentication not supported
- Eclipse shortcut for Compare With Head Revision
- How to profile and approach a slow PHP application
- How to debug PHP in eclipse in an external browser rather than internal?
- Eclipse - How to pin editor tabs?
- Error in Eclipse ADT Bundle :Failed to create a new project
- Question about Java Eclipse Export function
- How to configure a maven based java module project to run unit test in eclipse?
- Getting NoClassDefFoundError for BodyTagSupport with TomCat running from Eclipse
- Unable to Setup CoAP Server using Californium Java Library
- unable to see sqlite database in under app_database folder android emulator
- Get abs path from the currently edited file in Eclipse
- How to setup eclipse to work with code sourcery toolchain for arm
- Java compiler calls java.lang.ArrayIndexOutOfBoundsException without any reason
- Maven dependencies not being added to WEB-INF/lib