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.

536 questions
533
votes
51 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
  • 11,775
  • 20
  • 64
  • 80
26
votes
2 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
  • 601
  • 2
  • 8
  • 12
21
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…
21
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
  • 7,792
  • 20
  • 83
  • 132
17
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,491
  • 31
  • 126
  • 226
14
votes
2 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…
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
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…
10
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
  • 1,858
  • 2
  • 19
  • 32
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
  • 2,962
  • 5
  • 8
  • 35
7
votes
1 answer

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
  • 1,854
  • 2
  • 19
  • 41
7
votes
1 answer

Activity navigation: custom animation with popEnter and popExit like fragments

Changing activities with an animation is possible using the code below: Bundle animation = ActivityOptions.makeCustomAnimation(App.getContext(), R.anim.enter_from_right, R.anim.exit_to_left).toBundle(); startActivity(intent, animation); For…
6
votes
1 answer

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
  • 852
  • 11
  • 29
6
votes
1 answer

How to detect backbutton/home press from a service like messenger chat head service?

I have been looking through several stackoverflow question to find out how can I listen to backpress button on a service using windows manager. Most of the answers proposes that it's not possible, however I can see that messenger handle it very…
TSR
  • 9,145
  • 14
  • 51
  • 114
6
votes
5 answers

Android Double Back Press to close the app having fragments

I followed this tutorial and certain similar answers on SO. My present onBackPressed code is as follows - private static final int TIME_DELAY = 2000; private static long back_pressed; @Override public void onBackPressed() { DrawerLayout drawer…
Dr. Atul Tiwari
  • 933
  • 2
  • 18
  • 45
1
2 3
35 36