Questions tagged [onbackpressed]

onBackPressed is a function called in the Android operating system when the user presses the back key.

onBackPressed is a function called in the Android operating system when the user presses the back key. By default the operating system simply finishes the currently active Android Activity, but it is possible to override the function to do whatever you want, including keeping the current Activity open.

580 questions
626
votes
58 answers

How to implement onBackPressed() in Fragments?

Is there a way in which we can implement onBackPressed() in Android Fragment similar to the way in which we implement in Android Activity? As the Fragment lifecycle do not have onBackPressed(). Is there any other alternative method to over ride…
Android_programmer_camera
  • 13,197
  • 21
  • 67
  • 81
64
votes
8 answers

How to go back and refresh the previous page in Flutter?

I have a home page which when clicked takes me to another page through navigates, do some operations in then press the back button which takes me back to the home page. but the problem is the home page doesn't get refreshed. Is there a way to reload…
David
  • 1,071
  • 3
  • 11
  • 13
23
votes
2 answers

AutoCompleteTextView: Remove soft keyboard on back press instead of suggestions

When using AutoCompleteTextView, the dropdown suggestion list appears with the software keyboard still visible. This makes sense, as it is often a lot more efficient to type ensuing characters to narrow the list. But if the user wants to navigate…
20
votes
3 answers

Fragment PopBackStack

I am getting a strange problem, while using Fragments and popping back them out. I have one Fragment Activity: Step1: I am attaching one Fragment in the onCreate of that Activity in the Starting named Fragment A as: This is the onCreate of Fragment…
Gaurav Arora
  • 8,282
  • 21
  • 88
  • 143
18
votes
4 answers

In Android Navigation Architecture, how can I check if current Fragment is the last one?

I need to display custom AlertDialog, but only when there are no more fragments after calling NavController.navigateUp(). My current code does something similar, but there is a bug to it: override fun onBackPressed() { if…
18
votes
2 answers

Can not perform this action after onSaveInstanceState on super.onBackPressed()

I am displaying an interstitial ad when the user presses back to exit the application: mInterstitialAd.setAdListener(new AdListener() { @Override public void onAdClosed() { onBackPressed(); //line 98 …
erdomester
  • 11,789
  • 32
  • 132
  • 234
13
votes
1 answer

onBackPressedDispatcher.onBackPressed() vs backPressedCallback.handleOnBackPressed()

Since the old Activity.onBackPressed() becomes deprecated starting Android 33, what is the better way to call it programmatically? Example: override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { //…
12
votes
2 answers

How to detect if the Up button was pressed

In my activity the action bar shows only the left arrow and the title of the activity. When I press the left arrow the activity goes back to the previous activity, but no event is registered in the onKeyUp, OnkeyDown and OnBackPressed methods. But…
Zvi
  • 2,354
  • 2
  • 26
  • 37
12
votes
7 answers

Update selected state of navigation drawer after back press

Whats the proper way to handle the selected state of the navigation drawer after back press? I have a navigation drawer with n entries (in a listview) like the SDK sample in Android Studio. When i click on the navigation drawer entries i want them…
11
votes
3 answers

OnBackPressedCallback not called in Bottom Sheet Dialog Fragment

I have a Bottom Sheet Dialog Fragment which contains four Fragment with ViewPager. I want to call a method when onBackPressed clicked in Bottom Sheet Dialog Fragment. Implemented OnBackPressedCallback in my OnCreateView but it is not triggered. Any…
ysfcyln
  • 2,857
  • 6
  • 34
  • 61
11
votes
4 answers

Android reverse shared element transition on back after orientation change?

For shared element transition I am following this github project. It has 2 screens - one with recyclerview having number of cards & second the detail screen. As expected, it exhibits shared element transition of imageview & textview from…
8
votes
5 answers

Not working onbackpressed when setcancelable of alertdialog is false

I have an AlertDialog and its setCancelable() is false. In Onbackpressed function I want the AlertDialog to be closed. But when setCancelable() is false, the back key doesn't work at all. What should I do to get rid of this problem? I don't want to…
MMG
  • 3,226
  • 5
  • 16
  • 43
8
votes
2 answers

How to handle Android device BACK button press in Flutter?

How can I handle device back button's onPressed() in Flutter for Android? I know that I must put a back button manually for iOS but Android device has built-in BACK button and user can press it. How to handle that?
Mohsen Emami
  • 2,709
  • 3
  • 33
  • 40
7
votes
1 answer

Androidx OnBackPressedDispatcher - how to consume the back button press

I would like the onBackPressedDispatcher to absorb the back button press event. Sometimes but I don't see an option for it. Whats happening is that we are just today trying to upgrade to onBackPressedDispatcher in androidX but we have already…
j2emanue
  • 60,549
  • 65
  • 286
  • 456
7
votes
4 answers

Prevent Android Back Button Using Javascript

I have a simple HTML app with CSS and Javascript. I want to prevent the users from pressing the back button. I have achieved it using JavaScript and it works fine. My problem is that it doesn't work on Android devices, and when users press the…
1
2 3
38 39